winform c#写ini文件

INI文件就是扩展名为"ini"的文件。

其一般形式如下:
[section1]                             // 配置节
     //键名          //键值

   keyword1 = valuel
   keyword2 = value2
   ……
  [section2]
  keyword3 = value3
  keyword4 = value4

在Windows系统中,INI文件是很多,最重要的就是"System.ini"、"System32.ini"和"Win.ini"。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。以及XML文件的国际标准化给INI文件又一次打击。

但在某些场合,INI文件还拥有其不可替代的地位。比如绿色软件的规定就是不向注册表和系统中填入新东西。对于软件需要储存的信息就需要存入到文件中了。XML虽然兼容性比较好,但对于仅仅保存几个自定义参数而言就显得大材小用了。这是就可以选择使用快速简单的储存方式:INI文件。

本文就来探讨一下C#是如何对INI进行读写操作。

主要思路是调用Win32 API。

1.引入命名空间

using System.Runtime.InteropServices;
2.声明(把一个Win32 API函数转成C#函数)

//声明INI文件的写操作函数 WritePrivateProfileString()
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

//声明INI文件的读操作函数 GetPrivateProfileString()
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
3.函数


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();                  //注意类型的转换
}
到此基本功能已经实现了。下面我们将所有的代码重新整合一下:

namespace Library.File
{
    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对应

ps:最近看到有用的文章就转过来了,还是放在自己的园子里比较放心,转自 http://blog.163.com/luolin_110@126/blog/static/12671341920098391250127/

原文地址:https://www.cnblogs.com/wfwup/p/1616002.html