我有一个关于读取 ini 文件的问题,我需要读取我正在使用的 ini 文件的特定部分,但无法弄清楚如何执行此操作,我已经可以读取和写入 ini 文件,但我需要读取具体部分。
这是我的 INI 文件:
[Settings]
[ACR]
11: Start Removal =90 // ms
12: Removal Time =20 // commentary
13: Removal Delay =2.1 // commentary
[Cleaning]
21: Dur. Cleaning =90 //commentary
22: Time valve on =30 //commentary
23: Time valve off =15 //commentary
[Calibrate]
31: Content left =100//commentary
32: Calibrate left =--.-//commentary
33: Content right =100//commentary
34: Calibrate right =25.6//commentary
[Conductivity]
41: Factor left =500//commentary
42: Offset left =220//commentary
43: Factor right =500//commentary
44: Offset right =40//commentary
45: Level left =85//commentary
46: Level right =85//commentary
[General]
51: Type of valve =5//commentary
52: Indicator =2//commentary
53: Inverse output =0//commentary
54: Restart time =30//commentary
55: Water time =0//commentary
56: Gate delay =10//commentary
[Pulsation]
61: Pulsation p/m =60//commentary
62: S/r ratio front =55//commentary
63: S/r ratio back =60//commentary
64: Stimulation p/m =200//commentary
65: S/r stim front =30//commentary
66: S/r stim back =30//commentary
67: Stimulation dur =20//commentary
我必须阅读该行的前 2 个字符,所以在 ACR 部分下我需要阅读 10、11 和 12。在清理部分时,我必须阅读 21、22、23 等等。
到目前为止,这是我的代码:
using System;
using System.Windows.Forms;
using Idento.Common.Utilities;
using Milk_Units;
namespace Milk_Units
{
public class SettingsIniFile
{
private const String FileNameCustom = "Data\\Custom.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), );
private const String FileNameDefault = "Data\\Default.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), );
public Settings LoadSettings(bool defaults = false)
{
String fileName = defaults ? FileNameDefault : FileNameCustom;
StringList input = new StringList().FromFile(fileName);
//Settings settings = null;
Settings settings = new Settings();
foreach (var item in input)
{
String line = item.Trim();
if (line.StartsWith("[") && line.EndsWith("]"))
continue;
int index = line.IndexOf('=');
if (index < 0)
continue;
String key = line.Substring(0, index).Trim();
String value = line.Substring(index + 1).Trim();
String comment = "";
index = value.IndexOf("//");
if (index > -1)
{
comment = value.Substring(index).Trim();
value = value.Substring(0, index).Trim();
}
// ACR
if (Utils.EqualsIgnoreCase(key, "10: Start Removal"))
settings.AcrStartRemoval = value;
else if (Utils.EqualsIgnoreCase(key, "11: Removal Time"))
settings.AcrRemovalTime = value;
else if (Utils.EqualsIgnoreCase(key, "12: Removal Delay"))
settings.AcrRemovalDelay = value;
// CLEANING
else if (Utils.EqualsIgnoreCase(key, "21: Dur. Cleaning"))
settings.CleanDurCleaning = value;
else if (Utils.EqualsIgnoreCase(key, "22: Time valve on"))
settings.CleanTimeValveOn = value;
else if (Utils.EqualsIgnoreCase(key, "23: Time valve off"))
settings.CleanTimeValveOff = value;
//CALIBRATE
else if (Utils.EqualsIgnoreCase(key, "31: Content left"))
settings.CalibrateContentLeft = value;
else if (Utils.EqualsIgnoreCase(key, "32: Calibrate left"))
settings.CalibrateCalibrateLeft = value;
else if (Utils.EqualsIgnoreCase(key, "33: Content right"))
settings.CalibrateContentRight = value;
else if (Utils.EqualsIgnoreCase(key, "34: Calibrate right"))
settings.CalibrateCalibrateRight = value;
//CONDUCTIVITY
else if (Utils.EqualsIgnoreCase(key, "41: Factor left"))
settings.ConductFactorLeft = value;
else if (Utils.EqualsIgnoreCase(key, "42: Offset left"))
settings.ConductOffsetleft = value;
else if (Utils.EqualsIgnoreCase(key, "43: Factor right"))
settings.ConductFactorRight = value;
else if (Utils.EqualsIgnoreCase(key, "44: Offset right"))
settings.ConductOffsetRight = value;
else if (Utils.EqualsIgnoreCase(key, "45: Level left"))
settings.ConductLevelLeft = value;
else if (Utils.EqualsIgnoreCase(key, "46: Level right"))
settings.ConductLevelRight = value;
//GENERAL
else if (Utils.EqualsIgnoreCase(key, "51: Type of valve"))
settings.GeneralTypeOfValve = value;
else if (Utils.EqualsIgnoreCase(key, "52: Indicator"))
settings.GeneralIndicator = value;
else if (Utils.EqualsIgnoreCase(key, "53: Inverse output"))
settings.GeneralInverseOutput = value;
else if (Utils.EqualsIgnoreCase(key, "54: Restart time"))
settings.GeneralRestartTime = value;
else if (Utils.EqualsIgnoreCase(key, "55: Water time"))
settings.GeneralWaterTime = value;
else if (Utils.EqualsIgnoreCase(key, "56: Gate delay"))
settings.GeneralGateDelay = value;
//PULSATION
else if (Utils.EqualsIgnoreCase(key, "61: Pulsation p/m"))
settings.PulsationPulsationPm = value;
else if (Utils.EqualsIgnoreCase(key, "62: S/r ratio front"))
settings.PulsationSrRatioFront = value;
else if (Utils.EqualsIgnoreCase(key, "63: S/r ratio back"))
settings.PulsationSrRatioBack = value;
else if (Utils.EqualsIgnoreCase(key, "64: Stimulation p/m"))
settings.PulsationStimulationPm = value;
else if (Utils.EqualsIgnoreCase(key, "65: S/r stim front"))
settings.PulsationSrStimFront = value;
else if (Utils.EqualsIgnoreCase(key, "66: S/r stim back"))
settings.PulsationSrStimBack = value;
else if (Utils.EqualsIgnoreCase(key, "67: Stimulation dur"))
settings.PulsationStimulationDur = value;
}
return settings;
}
在此先感谢,我知道我没有正确使用 INI 文件,但这是最简单的方法。
Awnser 谢谢你 Neoistheone 的帮助
foreach (var item in input)
{
String line = item.Trim();
if (line.StartsWith("[") && line.EndsWith("]"))
continue;
int index = line.IndexOf('=');
if (index < 0)
continue;
String key = line.Substring(0, index).Trim();
String ID = line.Substring(0, line.IndexOf(':'));
String value = line.Substring(index + 1).Trim();
//String comment = "";
index = value.IndexOf("//");
if (index > -1)
{
ID = line.Substring(0, line.IndexOf(':'));
//comment = value.Substring(index).Trim();
value = value.Substring(0, index).Trim();
}
// ACR
if (Utils.EqualsIgnoreCase(key, "11: Start Removal"))
{
settings.AcrStartRemoval11 = value;
_settings.AcrId11.ID
}
返回设置;}