我正在尝试使用C#构建一个工具来修改我们软件的配置文件。配置文件采用以下格式:
SERVER_NAME TestServer
SERVER_IP 127.0.0.1
SERVICE_NUMBER 4
SERVICE_ID 1 2 3 4
等等。每行前面都有一个标识符(例如:SERVER_NAME),然后是值。我需要该工具将每个标识符的值加载到单独的文本框中。当用户单击“保存”时,需要将更新的信息写入文件。
我完全不知道如何将数据加载到文本框中,所以如果你能提供一些帮助,我将不胜感激。
写它,我假设最简单的方法,因为将加载所有数据,是擦除以前的数据,并将新数据写入文件。我应该能够毫无问题地处理。如果有更好的方法,我绝对愿意尝试。
我非常感谢有关如何开始加载数据的一些指示。
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFD.ShowDialog();
openFD.Title = "Open a Config File...";
openFD.InitialDirectory = "C:";
openFD.FileName = "";
openFD.Filter = "CONFIG|*.cfg";
string selected_file = "";
selected_file = openFD.FileName;
using (StreamReader sr = new StreamReader(selected_file))
{
string currLine;
while ((currLine = sr.ReadLine()) != null)
{
}
}
}
答案 0 :(得分:0)
要阅读文件的每一行,您可以执行以下操作:
// StreamReader is in System.IO
using(StreamReader sr = new StreamReader("config file path here"))
{
string currLine;
while((currLine = sr.ReadLine()) != null)
{
// currLine will have the current line value as a string
// You can then manipulate it any way you like
// Or store it in an array or List<>
}
}
如果您需要有关在项目框中添加项目的帮助,请询问。
希望这有帮助!