如何从C#中的INI文件中读取值?

时间:2022-10-16 04:32:48

I have a ini file that I want to read a line from under [DEVICE] tab and the following line DeviceName=PHJ01444-MC35 and assign the value to a string.

我有一个ini文件,我想从[DEVICE]选项卡和以下行DeviceName = PHJ01444-MC35中读取一行,并将值赋给字符串。

[BEGIN-DO NOT CHANGE/MOVE THIS LINE]
[ExecList]
Exec4=PilotMobileBackup v1;Ver="1.0";NoUninst=1
Exec3=MC35 108 U 02;Ver="1.0.8";NoUninst=1
Exec2=FixMGa;Ver="1.0";Bck=1
Exec1=Clear Log MC35;Ver="1.0";Bck=1
[Kiosk]
Menu8=\Program Files\PilotMobile\Pilot Mobile Backup.exe
MenuCount=8
AdminPwd=D85F72A85AE65A71BF3178CC378B260E
MenuName8=Pilot Mobile Backup
Menu7=\Windows\SimManager.exe
MenuName7=Sim Manager
UserPwd=AF2163B24AF45971
PasswordPolicy=C34B3DE916AA052DCB2A63D7DCE83F17
DisableBeam=0
DisableBT=0
DisableSDCard=0
EnableAS=1
ActionCount=0
Url=file://\Application\MCPortal.htz
AutoLaunch=0
Menu6=\Windows\solitare.exe
MenuName6=Solitare
Menu5=\Windows\bubblebreaker.exe
MenuName5=Bubble Breaker
Menu4=\Windows\wrlsmgr.exe
MenuName4=Communications
Menu3=\Windows\Calendar.exe
MenuName3=Calendar
Menu2=\Windows\tmail.exe
MenuName2=Text Messaging
Menu1=\Program Files\PilotMobile\Pilot.Mobile.exe
MenuName1=Pilot Mobile
ShowStartMenu=1
CustomTaskBar=0
IdleTimeout=0
NoTaskbar=0
PPCKeys=1111111111111111
On=1
[Status]
MCLastConn=2006/10/01 00:50:56
[Connection]
DeploySvr1=********
[Locations]
Backup=Backup
Install=\Application
[Comm]
RetryDelay=60000
NoInBoundConnect=0
TLS=0
Broadcast=1
[Info]
LID=090128-117
PWDID=081212-10
TimeSyncID={249CEE72-5918-4D18-BEA8-11E8D8D972BF}
TimeSyncErrorInterval=5
TimeSyncInterval=120
AutoTimeSync=1
SecondarySNTPServer=ntp1.uk.uu.net
DefaultSNTPServer=ntp0.uk.uu.net
DepServerTimeSyncType=4
TimeSyncServerType=1
DFID=080717-8
Platform=PPC
Method=39
SiteName=*****
[Device]
SyncTimer=4
Ver=1
DeviceID={040171BD-3603-6106-A800-FFFFFFFFFFFF}
ShowTrayIcon=1
DeviceIDType=2
DeviceClass=AADE7ECE-DF8C-4AFC-89D2-DE7C73B579D0
DeviceName=PHJ01444-MC35
NameType=2

[END-DO NOT CHANGE/MOVE THIS LINE]

4 个解决方案

#1


If you wanted the very simple but not very clean answer:

如果你想要一个非常简单但不是很干净的答案:

using System.IO;

StreamReader reader = new StreamReader(filename);
while(reader.ReadLine() != "[DEVICE]") { continue; }

const string DeviceNameString = "DeviceName=";
while(true) {
    string line = reader.ReadLine();
    if(line.Length < DeviceNameString.Length) { continue; }
    else if(line.Substring(0, DeviceNameString.Length) != DeviceNameString) { continue; }
    return line.Substring(DeviceNameString.Length);
}

If you're only intending to read one value from the file, it's a plausible option. I would probably combine the loops and add for some end of file checking though myself if you're serious about using this code.

如果您只想从文件中读取一个值,那么这是一个合理的选择。我可能会结合循环并添加文件检查的一些结束,如果你认真使用这段代码我自己。

#2


You could use Windows API for this. See http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/

您可以使用Windows API。见http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/

Edit: As noted in the comments the page is no longer available on the original site, however it's still accessible on the Wayback Machine.

编辑:如评论中所述,原始网站上的页面不再可用,但仍可在Wayback Machine*问。

Additionally there is a more recent article on MSDN about accessing the required functions.

此外,MSDN上有一篇关于访问所需功能的文章。

#3


Because writing everything in one line makes me a better person than you:

因为在一行中写下所有内容会让我成为比你更好的人:

string s = File.ReadAllText("inifile.ini").Split('\r', '\n').First(st => st.StartsWith("DeviceName"));

#4


string line;
string deviceName = string.Empty;
// Read the file and display it line by line.
using (System.IO.StreamReader file =
   new System.IO.StreamReader("c:\\file.ini"))
{
    while ((line = file.ReadLine()) != null)
    {
        if (line.ToLower().StartsWith("devicename"))
        {
            string[] fullName = line.Split('=');
            deviceName = fullName[1];
            break;
        }
    }
}

Console.WriteLine("Device Name =" + deviceName);
Console.ReadLine();

I am sure there are other ways.

我相信还有其他方法。

#1


If you wanted the very simple but not very clean answer:

如果你想要一个非常简单但不是很干净的答案:

using System.IO;

StreamReader reader = new StreamReader(filename);
while(reader.ReadLine() != "[DEVICE]") { continue; }

const string DeviceNameString = "DeviceName=";
while(true) {
    string line = reader.ReadLine();
    if(line.Length < DeviceNameString.Length) { continue; }
    else if(line.Substring(0, DeviceNameString.Length) != DeviceNameString) { continue; }
    return line.Substring(DeviceNameString.Length);
}

If you're only intending to read one value from the file, it's a plausible option. I would probably combine the loops and add for some end of file checking though myself if you're serious about using this code.

如果您只想从文件中读取一个值,那么这是一个合理的选择。我可能会结合循环并添加文件检查的一些结束,如果你认真使用这段代码我自己。

#2


You could use Windows API for this. See http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/

您可以使用Windows API。见http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/

Edit: As noted in the comments the page is no longer available on the original site, however it's still accessible on the Wayback Machine.

编辑:如评论中所述,原始网站上的页面不再可用,但仍可在Wayback Machine*问。

Additionally there is a more recent article on MSDN about accessing the required functions.

此外,MSDN上有一篇关于访问所需功能的文章。

#3


Because writing everything in one line makes me a better person than you:

因为在一行中写下所有内容会让我成为比你更好的人:

string s = File.ReadAllText("inifile.ini").Split('\r', '\n').First(st => st.StartsWith("DeviceName"));

#4


string line;
string deviceName = string.Empty;
// Read the file and display it line by line.
using (System.IO.StreamReader file =
   new System.IO.StreamReader("c:\\file.ini"))
{
    while ((line = file.ReadLine()) != null)
    {
        if (line.ToLower().StartsWith("devicename"))
        {
            string[] fullName = line.Split('=');
            deviceName = fullName[1];
            break;
        }
    }
}

Console.WriteLine("Device Name =" + deviceName);
Console.ReadLine();

I am sure there are other ways.

我相信还有其他方法。