如何写入文本文件的键和值并将它们读回? [英] How can write to a text file keys and values and read them back?

查看:27
本文介绍了如何写入文本文件的键和值并将它们读回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个新的表格

private void button1_Click(object sender, EventArgs e){button1.Enabled = false;label1.Text = "更新设置文件";label1.Visible = true;w = new StreamWriter(AuthenticationFileName,true);w.WriteLine(cTextBox1.Text);w.关闭();timer1.Start();}private void button4_Click(object sender, EventArgs e){button4.Enabled = false;label1.Text = "更新设置文件";label1.Visible = true;w = new StreamWriter(AuthenticationFileName,true);w.WriteLine(cTextBox2.Text);w.关闭();timer1.Start();}

然后在form1中:

string[] lines = File.ReadAllLines(Authentication.AuthenticationFileName);apiKey = 行 [0];用户 ID = 行 [1];jsonfiledirectory = 行 [2];

但问题是可能有一些情况 apiKey,userid 和 jsonfiledirectory 不会像现在这样顺序 [0] [1] [2]所以只是让 WriteLine 我想为每一行添加一个键和一个值,例如文本文件将是这样的:

apikey = 435345erfsefs54用户 ID = myttt@walla.comjsonfiledirectory = c:\

所以当我读回文本文件时,无论行的顺序如何.

解决方案

有很多解决方案可以保存设置,包括 xml 序列化您的类、使用 ini 文件、使用注册表、使用 json 文件、使用 appsettings ......

Windows 窗体应用程序的一种好方法是使用 应用程序设置.

通过这种方式,您可以使用设计器或使用代码创建设置类,然后在运行时加载设置、更改值以及保存或重置设置.

要使用设计器创建设置:

  1. 在解决方案资源管理器中,展开项目的属性节点并打开 Settings.settings 或根据需要添加新的设置文件.
  2. 在你需要的每一个设置的设计器中,你可以设置设置的NameValue为默认值,Type> 设置并选择 User 作为范围.这样您就可以在运行时更改设置.

  3. 您可以通过以下方式读取值、更改值或保存设置:

//读取并显示一个值MessageBox.Show(Properties.Settings.Default.Key1);//改变值Properties.Settings.Default.Key1 = "新值";//保存设置(您可以在设置表单或主表单的关闭事件中进行)Properties.Settings.Default.Save();

以编程方式创建设置:

  1. 向项目添加一个类并将其命名为 MySettings 并继承自 System.Configuration.ApplicationSettingsBase

  2. 为您需要的每个应用程序设置添加属性.添加 UserScopedSettingAttribute 到物业.您还可以使用 DefaultSettingValue 属性:

公共类 MySettings : System.Configuration.ApplicationSettingsBase{[UserScopedSetting()][DefaultSettingValue("Value1")]公共字符串 Key1{得到{return ((string)this["Key1"]);}放{this["Key1"] = 值;}}}

那么在使用的时候就可以

MySettings settnigs;private void Form1_Load(对象发送者,EventArgs e){settnigs=new MySettings();//读取并显示一个值MessageBox.Show(settnigs.Key1);//改变值settnigs.Key1 = "新值";}void Form1_FormClosing(对象发送者,FormClosingEventArgs{settnigs.Save();}

要了解有关设置的更多信息:

In a new form i did

private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            label1.Text = "Updating Settings File";
            label1.Visible = true;
            w = new StreamWriter(AuthenticationFileName,true);
            w.WriteLine(cTextBox1.Text);
            w.Close();
            timer1.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4.Enabled = false;
            label1.Text = "Updating Settings File";
            label1.Visible = true;
            w = new StreamWriter(AuthenticationFileName,true);
            w.WriteLine(cTextBox2.Text);
            w.Close();
            timer1.Start();
        }

Then in form1:

string[] lines = File.ReadAllLines(Authentication.AuthenticationFileName);
apiKey = lines[0];
userid = lines[1];
jsonfiledirectory = lines[2];

But the problem is that there might be cases the apiKey,userid and jsonfiledirectory will not be the same order like now [0] [1] [2] So instead just make WriteLine i want to add a key and a value to each line for example the text file will be like this:

apikey = 435345erfsefs54
userid = myttt@walla.com
jsonfiledirectory = c:\

So when i read back the text file no matter the order of the lines.

解决方案

There are many solutions to save settings including xml serializing your class, using ini files, using registry, using json files, using appsettings, ... .

One of good approaches for a windows forms application is using Application Settings.

This way you can create settings class using designer or using code and then at run-time, load settings, change values and save or reset settings.

To create settings using designer:

  1. In Solution Explorer, expand the Properties node of your project and open Settings.settings or add a new setting file if you need.
  2. In the designer for each setting that you need, you can set the Name of setting, Value as default value, Type for the setting and choose User as scope. This way you can change the setting at run-time.

  3. You can read values, change them or save settings this way:

//Read and show a value
MessageBox.Show(Properties.Settings.Default.Key1);

//Changes the value
Properties.Settings.Default.Key1 = "New Value";

//Save settings (You can do it in a setting form or in close event of your main form)
Properties.Settings.Default.Save();

To create settings programmatically:

  1. Add a class to the project and name it MySettings and inherit from System.Configuration.ApplicationSettingsBase

  2. Add properties for each application setting you need. Add UserScopedSettingAttribute to the property. You can also provide a default value for that property using DefaultSettingValue attribute:

public class MySettings : System.Configuration.ApplicationSettingsBase
{
    [UserScopedSetting()]
    [DefaultSettingValue("Value1")]
    public string Key1
    {
        get
        {
            return ((string)this["Key1"]);
        }
        set
        {
            this["Key1"] = value;
        }
    }
}

Then when using, you can

MySettings settnigs;

private void Form1_Load(object sender, EventArgs e)
{
    settnigs= new MySettings ();

    //Read and show a value
    MessageBox.Show(settnigs.Key1);

    //Changes the value
    settnigs.Key1 = "New Value";
}

void Form1_FormClosing(object sender, FormClosingEventArgs 
{
    settnigs.Save();
}

To learn more about settings:

这篇关于如何写入文本文件的键和值并将它们读回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆