code_junkie

How can I read a value from an INI file in C#

Nov 13th, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. [BEGIN-DO NOT CHANGE/MOVE THIS LINE]
  2. [ExecList]
  3. Exec4=PilotMobileBackup v1;Ver="1.0";NoUninst=1
  4. Exec3=MC35 108 U 02;Ver="1.0.8";NoUninst=1
  5. Exec2=FixMGa;Ver="1.0";Bck=1
  6. Exec1=Clear Log MC35;Ver="1.0";Bck=1
  7. [Kiosk]
  8. Menu8=Program FilesPilotMobilePilot Mobile Backup.exe
  9. MenuCount=8
  10. AdminPwd=D85F72A85AE65A71BF3178CC378B260E
  11. MenuName8=Pilot Mobile Backup
  12. Menu7=WindowsSimManager.exe
  13. MenuName7=Sim Manager
  14. UserPwd=AF2163B24AF45971
  15. PasswordPolicy=C34B3DE916AA052DCB2A63D7DCE83F17
  16. DisableBeam=0
  17. DisableBT=0
  18. DisableSDCard=0
  19. EnableAS=1
  20. ActionCount=0
  21. Url=file://ApplicationMCPortal.htz
  22. AutoLaunch=0
  23. Menu6=Windowssolitare.exe
  24. MenuName6=Solitare
  25. Menu5=Windowsbubblebreaker.exe
  26. MenuName5=Bubble Breaker
  27. Menu4=Windowswrlsmgr.exe
  28. MenuName4=Communications
  29. Menu3=WindowsCalendar.exe
  30. MenuName3=Calendar
  31. Menu2=Windowstmail.exe
  32. MenuName2=Text Messaging
  33. Menu1=Program FilesPilotMobilePilot.Mobile.exe
  34. MenuName1=Pilot Mobile
  35. ShowStartMenu=1
  36. CustomTaskBar=0
  37. IdleTimeout=0
  38. NoTaskbar=0
  39. PPCKeys=1111111111111111
  40. On=1
  41. [Status]
  42. MCLastConn=2006/10/01 00:50:56
  43. [Connection]
  44. DeploySvr1=********
  45. [Locations]
  46. Backup=Backup
  47. Install=Application
  48. [Comm]
  49. RetryDelay=60000
  50. NoInBoundConnect=0
  51. TLS=0
  52. Broadcast=1
  53. [Info]
  54. LID=090128-117
  55. PWDID=081212-10
  56. TimeSyncID={249CEE72-5918-4D18-BEA8-11E8D8D972BF}
  57. TimeSyncErrorInterval=5
  58. TimeSyncInterval=120
  59. AutoTimeSync=1
  60. SecondarySNTPServer=ntp1.uk.uu.net
  61. DefaultSNTPServer=ntp0.uk.uu.net
  62. DepServerTimeSyncType=4
  63. TimeSyncServerType=1
  64. DFID=080717-8
  65. Platform=PPC
  66. Method=39
  67. SiteName=*****
  68. [Device]
  69. SyncTimer=4
  70. Ver=1
  71. DeviceID={040171BD-3603-6106-A800-FFFFFFFFFFFF}
  72. ShowTrayIcon=1
  73. DeviceIDType=2
  74. DeviceClass=AADE7ECE-DF8C-4AFC-89D2-DE7C73B579D0
  75. DeviceName=PHJ01444-MC35
  76. NameType=2
  77.  
  78. [END-DO NOT CHANGE/MOVE THIS LINE]
  79. enter code here
  80.  
  81. using System.IO;
  82.  
  83. StreamReader reader = new StreamReader(filename);
  84. while(reader.ReadLine() != "[DEVICE]") { continue; }
  85.  
  86. const string DeviceNameString = "DeviceName=";
  87. while(true) {
  88. string line = reader.ReadLine();
  89. if(line.Length < DeviceNameString.Length) { continue; }
  90. else if(line.Substring(0, DeviceNameString.Length) != DeviceNameString) { continue; }
  91. return line.Substring(DeviceNameString.Length);
  92. }
  93.  
  94. string s = File.ReadAllText("inifile.ini").Split('r', 'n').First(st => st.StartsWith("DeviceName"));
  95.  
  96. string line;
  97. string deviceName = string.Empty;
  98. // Read the file and display it line by line.
  99. using (System.IO.StreamReader file =
  100. new System.IO.StreamReader("c:\file.ini"))
  101. {
  102. while ((line = file.ReadLine()) != null)
  103. {
  104. if (line.ToLower().StartsWith("devicename"))
  105. {
  106. string[] fullName = line.Split('=');
  107. deviceName = fullName[1];
  108. break;
  109. }
  110. }
  111. }
  112.  
  113. Console.WriteLine("Device Name =" + deviceName);
  114. Console.ReadLine();
Add Comment
Please, Sign In to add comment