cShap ini文件操作
时间: 2023-10-04 11:06:04 浏览: 100
在C#中,可以使用System.IO命名空间下的类来进行INI文件的读写操作。以下是一个简单的示例:
读取INI文件:
```csharp
using System.IO;
using System.Text;
class IniFile
{
private string filePath;
public IniFile(string filePath)
{
this.filePath = filePath;
}
public string ReadValue(string sectionName, string keyName)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(sectionName, keyName, "", sb, 255, filePath);
return sb.ToString();
}
[DllImport("kernel32")]
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
}
```
写入INI文件:
```csharp
using System.IO;
using System.Text;
class IniFile
{
private string filePath;
public IniFile(string filePath)
{
this.filePath = filePath;
}
public void WriteValue(string sectionName, string keyName, string value)
{
WritePrivateProfileString(sectionName, keyName, value, filePath);
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
}
```
使用示例:
```csharp
IniFile ini = new IniFile("test.ini");
ini.WriteValue("Section1", "Key1", "Value1");
string value = ini.ReadValue("Section1", "Key1");
Console.WriteLine(value);
```
输出:
```
Value1
```
阅读全文