3个回答
2013-06-04
展开全部
using System.Runtime.InteropServices; //函数作用:向INI文件中写信息,若文件不存在则新建一个
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
//函数作用:从私有文件中获取字符串(读Ini文件),返回读取到的字符串长度
//section:欲在其中查找条目的小节名称
//key:欲获取的项名或条目名
//def:指定的条目没有找到时返回的默认值。可设为空("")
//retVal:指定一个字串缓冲区,长度至少为size
//size:缓冲区大小
//filePath:INI文件的完整路径,若文件不存在也不会出错,返回0
//读取
private void button1_Click(object sender, EventArgs e)
{
string lujing = Application.StartupPath + "/CONFIG.INI";
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString("config", "password", "", temp, 255, lujing );
textBox1.Text = temp.ToString();
MessageBox.Show(i.ToString());
}
//写入
private void button2_Click(object sender, EventArgs e)
{
string lujing = Application.StartupPath + "/CONFIG.INI";
WritePrivateProfileString("config", "password", textBox2.Text , lujing );
}
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
//函数作用:从私有文件中获取字符串(读Ini文件),返回读取到的字符串长度
//section:欲在其中查找条目的小节名称
//key:欲获取的项名或条目名
//def:指定的条目没有找到时返回的默认值。可设为空("")
//retVal:指定一个字串缓冲区,长度至少为size
//size:缓冲区大小
//filePath:INI文件的完整路径,若文件不存在也不会出错,返回0
//读取
private void button1_Click(object sender, EventArgs e)
{
string lujing = Application.StartupPath + "/CONFIG.INI";
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString("config", "password", "", temp, 255, lujing );
textBox1.Text = temp.ToString();
MessageBox.Show(i.ToString());
}
//写入
private void button2_Click(object sender, EventArgs e)
{
string lujing = Application.StartupPath + "/CONFIG.INI";
WritePrivateProfileString("config", "password", textBox2.Text , lujing );
}
2013-06-04
展开全部
using System.Runtime.InteropServices; [DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); /// <summary>
/// 写入INI文件
/// </summary>
/// <param name=^Section^>节点名称</param>
/// <param name=^Key^>关键字</param>
/// <param name=^Value^>值</param>
/// <param name=^filepath^>INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name=^Section^>节点名称</param>
/// <param name=^Key^>关键字</param>
/// <param name=^filepath^>INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,255, filepath);
return temp.ToString();
}ini 文档格式路径假设为 D:/SETUP.ini[SQL]SVRName=192.168.1.11\SQL2005读取实例IniReadValue("SQL", "SVRName"," D:/SETUP.ini");这样读取出来的值是192.168.1.11\SQL2005写的话类似IniReadValue("SQL", "SVRName","你要写入的值"," D:/SETUP.ini");
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); /// <summary>
/// 写入INI文件
/// </summary>
/// <param name=^Section^>节点名称</param>
/// <param name=^Key^>关键字</param>
/// <param name=^Value^>值</param>
/// <param name=^filepath^>INI文件路径</param>
static public void IniWriteValue(string Section, string Key, string Value, string filepath)
{
WritePrivateProfileString(Section, Key, Value, filepath);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name=^Section^>节点名称</param>
/// <param name=^Key^>关键字</param>
/// <param name=^filepath^>INI文件路径</param>
/// <returns>值</returns>
static public string IniReadValue(string Section, string Key, string filepath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,255, filepath);
return temp.ToString();
}ini 文档格式路径假设为 D:/SETUP.ini[SQL]SVRName=192.168.1.11\SQL2005读取实例IniReadValue("SQL", "SVRName"," D:/SETUP.ini");这样读取出来的值是192.168.1.11\SQL2005写的话类似IniReadValue("SQL", "SVRName","你要写入的值"," D:/SETUP.ini");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-04
展开全部
有开源的项目代码啊,用起来不难的。有困难可以找下锐英源,有指导。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询