如何写入一个ini文件

本文关键字:一个 ini 文件 何写入 | 更新日期: 2023-09-27 18:18:54

我想知道我应该如何将文本写入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;
    }
}

}

如何写入一个ini文件

试试这个类:

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();
    }
  }

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