用C#如何读写配置文件?

用C#如何读写配置文件?,第1张

INI文件就是扩展名为"ini"的文件。\x0d\x0a其一般形式如下:\x0d\x0a[section1]// 配置节\x0d\x0a//键名 //键值\x0d\x0akeyword1 = valuel\x0d\x0akeyword2 = value2\x0d\x0a??\x0d\x0a[section2]\x0d\x0akeyword3 = value3\x0d\x0akeyword4 = value4\x0d\x0a在Windows系统中,INI文件是很多,最重要的就是"System.ini"、"System32.ini"和"Win.ini"。该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。以及XML文件的国际标准化给INI文件又一次打击。\x0d\x0a但在某些场合,INI文件还拥有其不可替代的地位。比如绿色软件的规定就是不向注册表和系统中填入新东西。对于软件需要储存的信息就需要存入到文件中了。XML虽然兼容性比较好,但对于仅仅保存几个自定义参数而言就显得大材小用了。这是就可以选择使用快速简单的储存方式:INI文件。\x0d\x0a本文就来探讨一下C#是如何对INI进行读写 *** 作。\x0d\x0a主要思路是调用Win32 API。\x0d\x0a1.引入命名空间\x0d\x0ausingSystem.Runtime.InteropServices\x0d\x0a2.声明(把一个Win32 API函数转成C#函数)\x0d\x0a//声明INI文件的写 *** 作函数 WritePrivateProfileString()\x0d\x0a[DllImport("kernel32")]\x0d\x0aprivate static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath)\x0d\x0a//声明INI文件的读 *** 作函数 GetPrivateProfileString()\x0d\x0a[DllImport("kernel32")]\x0d\x0aprivate static extern intGetPrivateProfileString(string section, string key, string def, StringBuilderretVal, int size, string filePath)\x0d\x0a3.函数\x0d\x0apublic void Writue(string section,string key, string value)\x0d\x0a{\x0d\x0a// section=配置节,key=键名,value=键值,path=路径\x0d\x0aWritePrivateProfileString(section,key, value, sPath)\x0d\x0a}\x0d\x0apublic string ReadValue(stringsection, string key)\x0d\x0a{\x0d\x0a// 每次从ini中读取多少字节\x0d\x0aSystem.Text.StringBuilder temp =new System.Text.StringBuilder(255)\x0d\x0a// section=配置节,key=键名,temp=上面,path=路径\x0d\x0aGetPrivateProfileString(section,key, "", temp, 255, sPath)\x0d\x0areturntemp.ToString()//注意类型的转换\x0d\x0a}\x0d\x0a到此基本功能已经实现了。下面我们将所有的代码重新整合一下:\x0d\x0anamespace Library.File\x0d\x0a{\x0d\x0apublic class Ini\x0d\x0a{\x0d\x0a// 声明INI文件的写 *** 作函数 WritePrivateProfileString()\x0d\x0a[System.Runtime.InteropServices.DllImport("kernel32")]\x0d\x0aprivate static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath)\x0d\x0a// 声明INI文件的读 *** 作函数 GetPrivateProfileString()\x0d\x0a[System.Runtime.InteropServices.DllImport("kernel32")]\x0d\x0aprivate static extern intGetPrivateProfileString(string section, string key, string def,System.Text.StringBuilder retVal, int size, string filePath)\x0d\x0aprivate string sPath = null\x0d\x0apublic Ini(string path)\x0d\x0a{\x0d\x0athis.sPath = path\x0d\x0a}\x0d\x0apublic void Writue(string section,string key, string value)\x0d\x0a{\x0d\x0a// section=配置节,key=键名,value=键值,path=路径\x0d\x0aWritePrivateProfileString(section,key, value, sPath)\x0d\x0a}\x0d\x0apublic string ReadValue(stringsection, string key)\x0d\x0a{\x0d\x0a// 每次从ini中读取多少字节\x0d\x0aSystem.Text.StringBuilder temp =new System.Text.StringBuilder(255)\x0d\x0a// section=配置节,key=键名,temp=上面,path=路径\x0d\x0aGetPrivateProfileString(section,key, "", temp, 255, sPath)\x0d\x0areturn temp.ToString()\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a开始调用函数。\x0d\x0a// 写入ini\x0d\x0aIni ini = newIni("C:/config.ini")\x0d\x0aini.Writue("Setting","key1", "HELLO WORLD!")\x0d\x0aini.Writue("Setting","key2", "HELLO CHINA!")\x0d\x0a// 读取ini\x0d\x0aIni ini = newIni("C:/config.ini")\x0d\x0astring str1 =ini.ReadValue("Setting", "key1")\x0d\x0aMessageBox.Show(str1)\x0d\x0a二,在一些小的应用中,有时候不需要使用数据困这样大规模的数据管理工具,也很少进行数据的查询、修改等 *** 作,而仅用文件来存储数据。这时就需要使用。net中的文件 *** 作对象,如file、streamReader、streamWriter等。\x0d\x0a1,使用File对象 *** 作文件\x0d\x0aSystem.IO.File类提供了一系类的静态办法,完成对晚间的常用 *** 作,如新建、删除、拷贝、移动等\x0d\x0a2,使用StreamWriter写入文件\x0d\x0a在System.IO空间中定义了一个文件写入器对象StreamWriter,使用它可以以一种特定的编码向输出流中(Stream)写入字符。\x0d\x0a3,使用SteamReader读取文件\x0d\x0a与streamWrite对应

配置文件在重要性不言而喻,在我们常用的软件中经常可以看到它的身影,它提供了程序初始化过程中一些常用的参数,并且可以手动的修改这些参数,因此使用起来非常的方便。常见的配置文件为*.ini文件。[小节名]关键字=值关键字=值……MFC为用户读取ini文件提供了几个函数,其中常用的几个函数分别如下:读取信息:GetPrivateProfileString和GetPrivateProfileInt写入信息:WritePrivateProfileString运用这几个函数就可以满足常用的对字符串和整数的读写 *** 作了。为了体现MFC的封装性以及方便使用,我们可以定义一个接口,即一个纯虚类。所有的方法都由这个接口继承而来。我们将这个纯虚类命名为CCfgFile,之后我们从这个纯虚类中继承一个类(CIniFile)用来实现对ini文件的读取。以后若是需要一些更高级的方法可以再从CCfgFile继承出其他的类来实现。这样我们就可以利用CIniFile类中定义的函数来 *** 纵ini文件了。在程序中我们需要 *** 作ini文件中一些常用的配置参数读写,我们可以定义一个参数类来实现,如CParam这里需要注意的是在程序中我们可能在很多地方都要实现配置参数的读写,我们不能在每个要使用的地方都通过new关键字来创建一个CParam对象。原因你懂的,呵呵!那么我们可以通过定义CParam的一个静态成员来实现,这个静态成员通过一个静态的成员函数来获取。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12049006.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存