用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对应

#include <stdio.h>

#include <string.h>

#define MAX_BUF 20

#define SERVER "localhost"

#define CONFIG_FILE "1.conf"

bool SetAuthServer(char* strServerAdd)

{

char buf[MAX_BUF], tempBuf[MAX_BUF]

memset(buf, 0, MAX_BUF)

memset(tempBuf, 0, MAX_BUF)

FILE *pF = fopen(CONFIG_FILE, "r")

if(!pF)

{

printf("打开文件失败!\n")

return false

}

fread(buf, MAX_BUF, 1, pF)

if(!feof(pF))

{

printf("读取不完整,请把MAX_BUF设置为大一点, 当前大小为: %d\n", MAX_BUF)

fclose(pF)

return false

}

fclose(pF)

char *lpPos = buf

char *lpNewPos = buf

while(lpNewPos = strstr(lpPos, SERVER))

{

strncpy(tempBuf+strlen(tempBuf), lpPos, lpNewPos-lpPos)

strcat(tempBuf, strServerAdd)

lpPos = lpNewPos + strlen(SERVER)

}

strcat(tempBuf, lpPos)

pF = fopen(CONFIG_FILE, "w")

if(!pF)

{

printf("打开文件失败!\n")

return false

}

fwrite(tempBuf, strlen(tempBuf), 1, pF)

fclose(pF)

return true

}

void main()

{

char buf[20]

printf("请输入一个字符串来修改服务器配置: ")

scanf("%s", buf)

if(SetAuthServer(buf) == true)

printf("修改成功!\n")

else

printf("修改失败!\n")

}

我想问下你的形参char *buf,是做什么的?,保存读取下来的字符串用的?

但是你下面要求转换进制,也就是说这些数据是数字性质,那应该是用int来保存阿 ?

另外你要保存的数据是len行吧,那就应该是int **才对咯

详细说明一下我的疑惑哈,现在已经帮你把数据都能够读出来了,就看你要怎么处理这些数据了

还有就是最后的转换,是要10进制保存下来,还是只需要以10进制输出到屏幕或者文件中即可

以字符串输出还是int输出,都要说明白


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

原文地址: http://outofmemory.cn/tougao/11685333.html

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

发表评论

登录后才能评论

评论列表(0条)

保存