0

我想知道我应该如何将文本写入 ini 文件。文件的读取已经完成。现在我只需要知道如何写入文件。

它在 INI 文件中时应如下所示:

[H83052_md7]
Description=H83048 processor mode 7
BootFile=kernel_52.md7
Extension=a20

如果您想查看我如何从文件中读取,请向我询问源代码,因为我无法弄清楚我应该如何做到这一点。

我这样写:

namespace Flashloader

{ class Controllerlist : List<Controller> { public Controllerlist FromIniFile() { StringList input = new StringList().FromFile("Controllers.ini");

        Controller controller = null;

        foreach (var item in input)
        {
            String line = item.Trim();

            if (line.StartsWith("[") && line.EndsWith("]"))
            {
                String name = line.Substring(1, line.Length - 2);

                controller = new Controller(name);
                Add(controller);
            }
            else if (controller != null)
            {
                int index = line.IndexOf('=');
                if (index < 0)
                    continue;

                String key = line.Substring(0, index).Trim();
                String value = line.Substring(index + 1).Trim();

                if (Utils.EqualsIgnoreCase(key, "Description"))
                    controller.Description = value;
                else if (Utils.EqualsIgnoreCase(key, "Bootfile"))
                    controller.Bootfile = value;
                else if (Utils.EqualsIgnoreCase(key, "Extension"))
                    controller.Extension = value;
            }
        }
        return this;
    }

    public Controller FindByName(String name)
    {
        foreach (var item in this)
            if (Utils.EqualsIgnoreCase(item.Name, name))
                return item;
        return null;
    }
}

}

4

2 回答 2

2

试试这个类:

public class IniFile
  {
    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
      string key,string val,string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
      string key,string def, StringBuilder retVal,
      int size,string filePath);

    public IniFile(string INIPath)
    {
      path = INIPath;
    }

    public void IniWriteValue(string Section,string Key,string Value)
    {
      WritePrivateProfileString(Section,Key,Value,this.path);
    }

    public string IniReadValue(string Section,string Key)
    {
      StringBuilder temp = new StringBuilder(255);
      int i = GetPrivateProfileString(Section,Key,"",temp,255, this.path);
      return temp.ToString();
    }
  }
于 2013-06-18T06:31:28.590 回答
1

这是写/读 INi 文件的最佳解决方案。http://www.blackwasp.co.uk/IniFile.aspx

于 2013-06-18T06:32:30.720 回答