引用:
using System.Runtime.InteropServices;
using System.Text
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
//参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。C#申明INI文件的读操作函数GetPrivateProfileString():
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
//参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
WritePrivateProfileString("1","姓名","张三",@"d:\b.ini");//写入
public string IniReadValue(string Section,string Key)//读取
{
StringBuilder temp = new StringBuilder(200);
int i = GetPrivateProfileString(Section,Key,"",temp,200,@"d:\b.ini");
return temp.ToString();
}
读写到记事本文件:
using System.IO;
//写入
StreamWriter sw = new StreamWriter( @"C:\temp123.txt");
sw.WriteLine("----------------hello----------------");
sw.WriteLine("内容");
sw.WriteLine("----------------hello----------------");
sw.Flush();
sw.Close();
//读取
System.IO.StreamReader st;
st = new System.IO.StreamReader(@"C:\temp123.txt", System.Text.Encoding.UTF8);//UTF8为编码
this.textBox1.Text = st.ReadToEnd();