繁体   English   中英

将配置文件加载到Windows窗体中

[英]Load Config file into Windows Forms

我正在尝试使用C#构建工具来修改我们软件的配置文件。 配置文件的格式如下:

SERVER_NAME测试服务器
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)
            {

            }
        }
    }

要读取文件的每一行,您可以执行以下操作:

// 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<>
    }
}

如果您需要将项目添加到文本框中的帮助,只需询问。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM