
{
public class Ini
{
// 声明INI文件的写 *** 作函数 WritePrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath)
// 声明INI文件的读 *** 作函数 GetPrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath)
private string sPath = null
public Ini(string path)
{
this.sPath = path
}
public void Writue(string section, string key, string value)
{
// section=配置节,key=键名,value=键值,path=路径
WritePrivateProfileString(section, key, value, sPath)
}
public string ReadValue(string section, string key)
{
// 每次从ini中读取多少字节
System.Text.StringBuilder temp = new System.Text.StringBuilder(255)
// section=配置节,key=键名,temp=上面,path=路径
GetPrivateProfileString(section, key, "", temp, 255, sPath)
return temp.ToString()
}
}
}
开始调用函数。
// 写入ini
Ini ini = new Ini("C:/config.ini")
ini.Writue("Setting", "key1", "HELLO WORLD!")
ini.Writue("Setting", "key2", "HELLO CHINA!")
// 读取ini
Ini ini = new Ini("C:/config.ini")
string str1 = ini.ReadValue("Setting", "key1")
MessageBox.Show(str1)
二,在一些小的应用中,有时候不需要使用数据困这样大规模的数据管理工具,也很少进行数据的查询、修改等 *** 作,而仅用文件来存储数据。这时就需要使用。net中的文件 *** 作对象,如file、streamReader、streamWriter等。
1,使用File对象 *** 作文件
System.IO.File类提供了一系类的静态办法,完成对晚间的常用 *** 作,如新建、删除、拷贝、移动等
2,使用StreamWriter写入文件
在System.IO空间中定义了一个文件写入器对象StreamWriter,使用它可以以一种特定的编码向输出流中(Stream)写入字符。
3,使用SteamReader 读取文件
与streamWrite对应
文章转载自网管之家:http://www.bitscn.com/pdb/dotnet/201003/181956_2.html
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)