簡體   English   中英

如何將我的應用程序設置保存到文本文件並在加載時重新讀取?

[英]How can i save settings of my app to a text file and read them back on load?

在應用程序構造函數中以及在程序的其他位置重新讀取它們。 我有一個使用某些復選框創建的新表格:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Options
{
    public partial class OptionsMenuForm : Form
    {


        public OptionsMenuForm()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                Settings.downloadonstart = true;
            }
            else
            {
                Settings.downloadonstart = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                Settings.loadonstart = true;
            }
            else
            {
                Settings.loadonstart = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                Settings.startminimized = true;
            }
            else
            {
                Settings.startminimized = false;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                Settings.displaynotifications = true;
            }
            else
            {
                Settings.displaynotifications = false;
            }
        }
    }
}

現在我想每次保存一個/任何復選框的狀態。 我還添加了一個我正在使用的類,該類在新表格和form1之間傳遞變量:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Options
{
    class Settings
    {
        public static bool downloadonstart;
        public static bool loadonstart;
        public static bool startminimized;
        public static bool displaynotifications;
    }
}

現在如何通過將設置保存到文本文件來在form1中使用它? 例如,在文本文件中,內容將類似於:

CheckBox1 = true
CheckBox2 = false
CheckBox3 = false
CheckBox4 = true

然后,如果我更改其中之一的狀態,則會將其寫入文本文件:

CheckBox1 = false
CheckBox2 = false
CheckBox3 = true
CheckBox4 = true

在form1頂部,我添加了

string settingsFile = "settings.txt";
string settingsFileDirectory = "\\settings";
StreamWriter writetosettingsfile;

然后在構造函數中

settingsFileDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) +
                settingsFileDirectory;
            if (!Directory.Exists(settingsFileDirectory))
            {
                Directory.CreateDirectory(settingsFileDirectory);
            }

我知道該應用程序本身在屬性中具有設置,但是由於我有很多設置,並且這次可能還會有更多設置,所以我這次想使用文本文件。

或使用我在屬性中應用程序中的設置:

設置

但是現在如何在程序中使用它來將每個復選框狀態保存為新表單,然后在form1中使用它?

您可以使用json.net

像這樣保存數據

private void Form1_Load(object sender, EventArgs e)
{
    checkBox1.CheckedChanged += checkBox_CheckedChanged;
    checkBox2.CheckedChanged += checkBox_CheckedChanged;
    checkBox3.CheckedChanged += checkBox_CheckedChanged;
    checkBox4.CheckedChanged += checkBox_CheckedChanged;
}

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    var settings = new Settings();
    settings.downloadonstart = checkBox1.Checked;
    settings.loadonstart = checkBox2.Checked;
    settings.startminimized = checkBox3.Checked;
    settings.displaynotifications = checkBox4.Checked;
    File.WriteAllText(@"c:\configfile.json", JsonConvert.SerializeObject(settings));
}

您可以像這樣讀取文件

Settings settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(@"c:\configfile.json"));

文檔: http : //www.newtonsoft.com/json/help/html/Introduction.htm

public OptionsMenuForm()
{
    InitializeComponent();
    //Read the settings from a file with comma delimited 1's and 0's or whatever you like
    string[] values = System.IO.File.ReadAllText("c:\temp\a.txt").Split(',');
    checkBox1.Checked = Convert.ToBoolean(Convert.ToInt32(values[0]));
    checkBox2.Checked = Convert.ToBoolean(Convert.ToInt32(values[1]));;

    //On checkbox changes save the settings
    checkBox1.CheckedChanged +=SaveSettings;
    checkBox2.CheckedChanged +=SaveSettings;
}

public void SaveSettings(object sender,EventArgs e)
{
    StringBuilder sbValues = new StringBuilder();
    int i = 0;

    i = checkBox1.Checked ? 1 : 0;
    sbValues.Append(i.ToString() + ",");

    i = checkBox2.Checked ? 1 : 0;
    sbValues.Append(i.ToString() + ",");

    System.IO.File.WriteAllText("c:\temp\a.txt",sbValues.ToString());
}

建議您在使用.NET框架時使用XML。

public static void ConvertStringToXmlList()
{
    XElement xml = new XElement
        (
            "Items",
            (
                from x in [YourList] select new XElement("Item", x)
            )
        );
    XDocument doc = new XDocument
        (
            xml
        );
    doc.Save(Environment.CurrentDirectory + "/settings.xml");  
}



var xmlReader = new XmlTextReader("settings.xml");
while (xmlReader.Read()) 
{
    switch (reader.NodeType) 
    {
       case [nodetype]:
           // Code here
       break;
    }
}

另外,不要為所有四個復選框都創建相同的事件,而只需使用1。

在單個事件內部:

ckCheckBox1.Checked = !ckCheckBox1.Checked;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM