我是一个.NET新手,遇到一个问题,想请教高手
如何读写.NET中的ini文档,如何创建ini文档?
如何读写.NET中的ini文档,如何创建ini文档?
解决方案 »
- winform 动态改变picturebox的位置 界面闪动
- 求助:在从服务器接收结果时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 信号灯超时时间已到)
- 关于文件保存的文件名问题??!!
- 怎样取得某个Class自身的实例名称?
- 求救:C# 里如何编写一小断程序来判断 某个下载地址是否有效
- Thread.Join ()
- 求助关于在c#下发送get请求的问题
- 怎样子使用c#连接公网上的sql server 数据库
- 怎么阻止应用程序的多重启动?
- 如何在生成一个文件后让文件自动下载到本地硬盘,是要可选择路径的那种!
- 已知文件名,获得它的路径
- 在ASP.NET中如何实现文字滚动?
http://www.codeproject.com/csharp/readwritexmlini.asp
//调用WindowsAPI函数来读取INI配置文件
[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 siae,string filePath);
//读取配置文件
public static int loadConfig(string path)
{
int sign=0; //设置返回值,用于判断读取配置文件是否出错
StringBuilder temp = new StringBuilder(255);
string FileName = path;//设置配置文件路径
string section = "IP地址";//设置"段"的名称
string key = "IP";//设置"关键字"的名称
GetPrivateProfileString(section, key, "noValue", temp, 255, FileName) ;//读取"关键字"的值
ip=temp.ToString ();
if(ip=="noValue")//如果读取出错,返回出错
{
ip="";
return sign;
}
section="用户名";
key="UserName";
GetPrivateProfileString(section, key, "noValue", temp, 255, FileName) ;
connectUsername=temp.ToString ();
if(connectUsername=="noValue")
{
connectUsername="";
return sign;
} section="密码";
key="Password";
GetPrivateProfileString(section, key, "noValue", temp, 255, FileName) ;
connectPassword=temp.ToString ();
if(connectPassword=="noValue")
{
connectPassword="";
return sign;
}
sign=1;
return sign;
} //保存配置文件
public static void storageFile(string filePath,string ipaddress,string name,string password)
{
string FileName = filePath;
string section = "IP地址";
string key = "IP";
string keyValue=ipaddress;
WritePrivateProfileString (section, key, keyValue, FileName);
section="用户名";
key="UserName";
keyValue=name;
WritePrivateProfileString (section, key, keyValue, FileName); section="密码";
key="Password";
keyValue=password;
WritePrivateProfileString (section, key, keyValue, FileName);
} }