C# 如何读写 ini 文档

INI文档如下:

[MyApp01]
InstallDir=C:\MyApp\01

1)要求读取 InstallDir的值
2)插入一个字段 Main=01.exe
3)修改Main:Main=02.apk
4)移除Main

纯手写代码 非copy
下面有测试连接 在我自己的服务器上...有时候服务器会出问题.....
代码:http://help.kansea.com/zhidao/csharp/298037741.html
调用方法
myini = new _Ini("ini文件地址");
myini.IniWriteValue("Main","01.exe");
string str = myini.IniReadValue(InstallDir);

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace KanSea.XBATools
{
class _Ini
{
public string path;   //INI文件名
public _Ini(string inipath)
{
//this.path = System.Environment.CurrentDirectory + "\\KanSea_XBA.ini";
this.path = inipath;
}
//声明读写INI文件的API函数
[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);
//写INI文件   
public void IniWriteValue(string Key, string Value)
{
WritePrivateProfileString("中括号字符 MyApp01 ", Key, Value, this.path);
}
//读取INI文件指定
public string IniReadValue(string Key)
{
try
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString("MyApp01", Key, "", temp, 255, this.path);

return temp.ToString().Trim();
}
catch { return null; }
}
}
}追问

我试过这种方法,结果是:
iniFile.ini
文件大小:0字节

追答

我一直都用这个源码.....怎么会出错

我的程序http://help.kansea.com/zhidao/csharp/298037741.7z

追问

您访问的页面已删除或不存在

追答

你说你试过 是试过我的代码么 我觉得不是

还有 上面说过 "下面有测试连接 在我自己的服务器上...有时候服务器会出问题....."

你试试看

代码方面 你要适当修改一下
"中括号字符 MyApp01 "和"MyApp01" 换成你自己的 或者设成用变量传

我的程序的截图

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-26
MyApp01]
InstallDir=C:\MyApp\01

1)要求读取 InstallDir的值
2)插入一个字段 Main=01.exe
3)修改Main:Main=02.apk
4)移除Main追问

等于没答

第2个回答  2011-07-27
不会。。
相似回答