1.c#如何写配置参数文件
建议写入文本文件,文本文件格式可以自定义为xml、conf、config等各类文件类型
文本文件格式建议使用xml文件格式如
<config version=???>
<app>xxx</app>
<xxx>xxx</xxx>
<home><x1>xxx</x1><x2>xxx</x2></home>;等等
2.用C#如何读写配置文件
INI文件就是扩展名为"ini"的文件。
其一般形式如下:[section1] // 配置节//键名 //键值keyword1 = valuelkeyword2 = value2……[section2]keyword3 = value3keyword4 = 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.引入命名空间usingSystem.Runtime.InteropServices;2.声明(把一个Win32 API函数转成C#函数)//声明INI文件的写操作函数 WritePrivateProfileString()[DllImport("kernel32")]private static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);//声明INI文件的读操作函数 GetPrivateProfileString()[DllImport("kernel32")]private static extern intGetPrivateProfileString(string section, string key, string def, StringBuilderretVal, 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(stringsection, string key){// 每次从ini中读取多少字节System.Text.StringBuilder temp =new System.Text.StringBuilder(255);// section=配置节,key=键名,temp=上面,path=路径GetPrivateProfileString(section,key, "", temp, 255, sPath);returntemp.ToString(); //注意类型的转换}到此基本功能已经实现了。下面我们将所有的代码重新整合一下:namespace Library.File{public class Ini{// 声明INI文件的写操作函数 WritePrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);// 声明INI文件的读操作函数 GetPrivateProfileString()[System.Runtime.InteropServices.DllImport("kernel32")]private static extern intGetPrivateProfileString(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(stringsection, 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();}}} 开始调用函数。
// 写入iniIni ini = newIni("C:/config.ini");ini.Writue("Setting","key1", "HELLO WORLD!");ini.Writue("Setting","key2", "HELLO CHINA!");// 读取iniIni ini = newIni("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对应。
3.C#如何读写app.config文件
<configuration>
.
<connectionStrings>
<add name="connectionName" connectionString="server=.;database=MusicStore;uid=sa;pwd=abc;" providerName="System.Data.SqlClient"/>
</connectionStrings>
..
</configuration>
你的zhidao[connectionName]要加引专号属
4.C:\windows\system32\config\怎么打开
方法一:
复制C:\windows\system32\config\,打开我的电脑,在地址里面粘贴复制C:\windows\system32\config\,回车即可打开。
方法二:
1、根据文件路径,打开我的电脑双击打开C盘。
2、双击打开windows文件夹,找到system32,双击打开。
3、找到config文件夹,双击打开即可。
5.C:\WINDOWS\system32中的config文件夹是什么啊
系统存放 的密码 文件
sam
SAM文件基础知识
windows NT及win2000中对用户帐户的安全管理使用了安全帐号管理器(security account manager)的机制,安全帐号管理器对帐号的管理是通过安全标识进行的,安全标识在帐号创建时就同时创建,一旦帐号被删除,安全标识也同时被删除。安全标识是唯一的,即使是相同的用户名,在每次创建时获得的安全标识都时完全不同的。因此,一旦某个帐号被删除,它的安全标识就不再存在了,即使用相同的用户名重建帐号,也会被赋予不同的安全标识,不会保留原来的权限。
安全账号管理器的具体表现就是%SystemRoot%\system32\config\sam文件。sam文件是windows NT的用户帐户数据库,所有NT用户的登录名及口令等相关信息都会保存在这个文件中。sam文件可以认为类似于unix系统中的passwd文件,不过没有这么直观明了。
SAM文件是WIN2000里面保存密码信息的文件 WinXP的SAM文件位于C:\Windows/system32/config/下,如果SAM文件损坏或丢失了,WinXP中的所有帐号就会丢失,而且还无法通过WinXP登录界面。要恢复SAM文件,你可以把Win XP安装光盘上的sam文件(H:\windows\repair\sam)复制到C:\Windows/system32/config/下即可。即打开命令行模式,输入copy H:\windows\repair\sam C:\windows\system32\config\sam回车,就大功告成了。
如果你安装XP后,还创建了其他用户和用户组,按以上方法操作之后,这些帐号就会丢失啦,因为现在的Sam文件,只含有安装WinXP时产生的帐号。为此,你可以先登录WinXP,点击“开始”/程序/附件/系统工具/系统还原,把系统还原到最新的还原点、如此恢复这些用户设置。如解除密码的话就要以下操作,从DOS下运行解密软件,把SAM里存有的密码清除掉,现在的GHOSTXP盘大多集成了解密软件: Win2000==>C:\winnt\system32\config\sam
winXP==>C:\windows\system32\config\sam
6.c语言读写配置文件
#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");
}
7.c:\windows\system32\config
AppEvent.Evt---这是应用程序的事件查看器的“应用程序日志”对应.
SecEvent.Evt--在事件查看器中的“安全日志”项对应。
DEFAULT--这是默认用户的配置信息,在注册表中对应[Hkey_users/.default]项目。
SAM--是用来存储用户的密码的。对应于注册表的[HKEY_LOCAL_MACHINE\SAM]项目。
SECURITY--对应于注册表中的[HKEY_LOCAL_MACHINE\SECURITY]项目!
SOFTWARE--对应于注册表中的[HKEY_LOCAL_MACHINE\SOFTWARE],记录你的计算机上安装的软件,也包括微软的软件在内!
SYSTEM--对应于注册表中的[HKEY_LOCAL_MACHINE\System]项目!
SYSTEM.ALT--此项目可能是在事件查看器中的“系统日志”项对应巴!
转载请注明出处育才学习网 » c怎么写config文件