读取(写入)配置文件

引用:

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();


   本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

原文地址:https://www.cnblogs.com/wzg0319/p/1630854.html