日期:2014-05-20  浏览次数:20763 次

程序的可变参数保存的方法(Reg or INI)
以前用BCB   喜欢把参数保存在INI   文件,
用EVC喜欢保存在注册表,
现在学用C#,   听说可以保存在程序本身,不明白.....?

请问什么原理?谢谢!

------解决方案--------------------
不知道有这个方法。。。
个人见解,最好不使用注册表。使用文件方式比较好。软件的全局参数放一个文件,项目参数一个文件。这样重启设备也不会丢失参数。
------解决方案--------------------
txt文件怎么可能错嘛,调试好就不会错了啊。
很容易吧,比如你写文件的时候可以这样:
参数1[值]参数2[值]参数3[值]参数4[值]....
读取的时候就这样:
一次性都读过来,然后写个正则
参数1\[(? <参数1> [^\]]+?)参数2\[(? <参数2> [^\]]+?).....
然后根据对应的组获取数据即可,例如:
IntParameter1 = int.Parse(result.Group[ "参数1 "].Value);
------解决方案--------------------
写个例子给你,在界面上,放2个按钮,btnWrite,btnRead,放一个文本框,默认名字即可。事件响应函数如下:
//写配置文件
private void btnWrite_Click(object sender, EventArgs e)
{
int iMyVariable = 0;
if (int.TryParse(textBox1.Text, out iMyVariable))
{
System.IO.FileInfo fiSettingFile = new System.IO.FileInfo(@ "c:\MySetting.txt ");

System.IO.StreamWriter sw = fiSettingFile.CreateText();
string strSetting = string.Format( "TextValue1[{0:d}]TextValue2[{1:d}] ",
iMyVariable, iMyVariable + 5);
sw.Write(strSetting);
sw.Flush();
sw.Close();
MessageBox.Show( "Save setting done! ");
}
}

//读配置文件
private void btnRead_Click(object sender, EventArgs e)
{
System.IO.FileInfo fiSettingFile = new System.IO.FileInfo(@ "c:\MySetting.txt ");
if (!fiSettingFile.Exists || 0 == fiSettingFile.Length)
{
MessageBox.Show( "Setting file missing.Use default setting now. ");
textBox1.Text = "0 ";
return;
}

System.IO.StreamReader sr = fiSettingFile.OpenText();
string strSetging = sr.ReadToEnd();
System.Text.RegularExpressions.Match result =
System.Text.RegularExpressions.Regex.Match(strSetging,
@ "TextValue1\[(? <value1> [^]]+?)\]TextValue2\[(? <value2> [^]]+?)\] ");
if (result.Success)
{
textBox1.Text = result.Groups[ "value1 "].Value;//update view control
string strTest = result.Groups[ "value2 "].Value;
int iMyVariable = 0;
if (int.TryParse(result.Groups[ "value1 "].Value, out iMyVariable))
{
string strTest2 = string.Format( "value1:{0:d}\r\nvalue2:{1:s} ",
iMyVariable,strTest);
MessageBox.Show( "get data successful!\r\nThese are :\r\n " + strTest2);
}
}
}
------解决方案--------------------
//注册表读写通过Registrykey实现
using Microsoft.Win32;

RegistryKey vRegistryKey = Registry.CurrentUser.OpenSubKey(
@ ".DEFAULT ", true);
vRegistryKey.SetValue( "Note ", "Zswang 路过 ");

MessageBox.Show(vRegistryKey.GetValue( "Note ").ToString());

------解决方案--------------------
保存在程序本身? 可以,保存在资源文件里,然后把资源文件嵌入dll中.
------解决方案--------------------