如何将C#代码写入settings.cfg文件? [英] How do I write C# code to a settings.cfg File?
问题描述
您好,
我正在开发一个所有用户互动的程序。
I am working on a program that is all user interactive.
我几乎完成了它,但我我不希望它有任何错误。
I almost have it done, but i do not want it to have any bugs.
我希望用户能够以他们想要的任何顺序设置任何标题栏名称或将程序设置为按钮。
I want the user to be able to set any title bar name or set a program to a button, in any order they want.
例如:
在settings.cfg文件中我也使用索引来写项目。
in the settings.cfg file I use index's to write items too.
titlebar1.Text转到索引[0]
titlebar1.Text goes to index[0]
程序button1的路径包括文件名转到索引[1]
Program button1's path including filename goes to index[1]
程序按钮2的包含文件名的路径转到索引[2]
ECT:
以下是我的尝试:
if ((this.TextBox1.Text != "") || (this.p.custompath1 != null))
{
string[] cp1 = new string[] { this.TextBox1.Text, this.p.custompath1 };
this.TextBox1.Text = cp1[0];
this.p.custompath1 = cp1[1];
File.WriteAllLines(this.currentpath + @"\settings.cfg", cp1);
this.TitleBar1.Text = this.TextBox1.Text;
this.TextBox1.Visible = false;
MessageBox.Show("Name entered successfully");
}
if (((this.TextBox1.Text != "") || (this.p.custompath1 != null) || (this.p.custompath2 != null)))
{
string[] cp2 = new string[] { "[ " + this.TextBox1.Text + " ]", this.p.custompath1, this.p.custompath2 };
this.TextBox1.Text = cp2[0];
this.p.custompath1 = cp2[1];
this.p.custompath2 = cp2[2];
File.WriteAllLines(this.currentpath + @"\settings.cfg", cp2);
this.TitleBar1.Text = this.TextBox1.Text;
this.TextBox1.Visible = false;
MessageBox.Show("Name entered successfully");
}
if ((((this.TextBox1.Text != "") || (this.p.custompath1 != null) || (this.p.custompath2 != null) || (this.p.custompath3 != null))))
{
string[] cp3 = new string[] { "[ " + this.TextBox1.Text + " ]", this.p.custompath1, this.p.custompath2, this.p.custompath3 };
this.TextBox1.Text = cp3[0];
this.p.custompath1 = cp3[1];
this.p.custompath2 = cp3[2];
this.p.custompath3 = cp3[3];
File.WriteAllLines(this.currentpath + @"\settings.cfg", cp3);
this.TitleBar1.Text = this.TextBox1.Text;
this.TextBox1.Visible = false;
MessageBox.Show("Name entered successfully");
}
if (((((this.TextBox1.Text != "") || (this.p.custompath1 != null) || (this.p.custompath2 != null) || (this.p.custompath3 != null) || (this.p.custompath4 != null)))))
{
string[] cp4 = new string[] { "[ " + this.TextBox1.Text + " ]", this.p.custompath1, this.p.custompath2, this.p.custompath3, this.p.custompath4 };
this.TextBox1.Text = cp4[0];
this.p.custompath1 = cp4[1];
this.p.custompath2 = cp4[2];
this.p.custompath3 = cp4[3];
this.p.custompath4 = cp4[4];
File.WriteAllLines(this.currentpath + @"\settings.cfg", cp4);
this.TitleBar1.Text = this.TextBox1.Text;
this.TextBox1.Visible = false;
MessageBox.Show("Name entered successfully");
}
if ((((((this.TextBox1.Text != "") || (this.p.custompath1 != null) || (this.p.custompath2 != null) || (this.p.custompath3 != null) || (this.p.custompath4 != null) || (this.p.custompath5 != null))))))
{
string[] cp5 = new string[] { "[ " + this.TextBox1.Text + " ]", this.p.custompath1, this.p.custompath2, this.p.custompath3, this.p.custompath4, this.p.custompath5 };
this.TextBox1.Text = cp5[0];
this.p.custompath1 = cp5[1];
this.p.custompath2 = cp5[2];
this.p.custompath3 = cp5[3];
this.p.custompath4 = cp5[4];
this.p.custompath5 = cp5[5];
File.WriteAllLines(this.currentpath + @"\settings.cfg", cp5);
this.TitleBar1.Text = this.TextBox1.Text;
this.TextBox1.Visible = false;
MessageBox.Show("Name entered successfully");
}
但它不起作用
请帮助
推荐答案
您的settings.cfg文件是什么格式在?通常建议您使用现有的一种设置格式(例如
用户设置,
JSON 或
XML )或创建一个类型来存储配置数据,然后将该类型序列化为单个块。使用数组很麻烦,容易出错,并且不能让你灵活地在以后进行更改。
What format is your settings.cfg file in? It is generally recommended that you either use one of the existing settings formats (like user settings, JSON or XML) or create a type to store the configuration data and then serialize that type out as a single chunk. Using arrays is messy, error prone and doesn't give you the flexibility to make changes later.
public class UserSettings
{
public string Version { get; set; }
public string Title { get;set; }
//Can be as simple or complex as you want
public string[] Programs { get;set; }
}
public static class SettingsFile
{
public UserSettings Load ( string filename )
{
//Read the file in whatever format you are using - XML, JSON, etc.
//Should read the version first and then load based upon the version since
//a saved settings file may be earlier than what your program supports
}
public void Save ( string filename, UserSettings settings )
{
//Save the file in whatever format you are using - XML, JSON, etc.
}
}
在任何你想保存用户内容的地方。
In whatever place you want to save the user's stuff.
var settings = new UserSettings()
{
Version = "1.0",
Title = TextBox1.Text,
//If this is a hardcoded # of programs...
Programs = new [] {
p.custompath1,
p.custompath2,
p.custompath3,
p.custompath4,
p.custompath5
}
};
SettingsFile.Save(filename, settings);
加载设置。
var settings = SettingsFile.Load(filename);
TextBox1.Text = settings.Title;
//This is pretty ugly to do but when you are purposefully building your app in this manner there is little workaround
p.custompath1 = (settings.Programs?.Length > 0) ? settings.Programs[0] : null;
p.custompath2 = (settings.Programs?.Length > 1) ? settings.Programs[1] : null;
...
这篇关于如何将C#代码写入settings.cfg文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!