控制台应用程序的C#设置文件

问题描述:

所以我想创建一个存储可以编辑的特定信息的文件,如:

(lvl2 = false)

So I want to make a file that stores specific information that can be edited like:
(lvl2 = false)

//Just imagine that the user just wanted to play level 2

if (lvl2 = false){
Console.Writeline("Sorry but you didn't unlock level 2 yet");

if (lvl2 = true){
Console.Writeline("You entered level 2...");
//Here the user gets redirected to level 2
}



所以我想到了一个设置文件。无论是XML还是普通项目>添加属性>配置文件方法都没关系,我只是不知道如何从中检索数据。



做什么如果我想从设置文件中检查lvl2 = true,我必须这样做吗?



感谢您的每一个有用答案。



我尝试过:



我试过


So I thought of a settingfile. It does not matter if its XML or just the normal project>add attribute>configurationfile method, I just don't get how to retrieve data from it.

What do I have to do if I want to check if lvl2 = true from the settingfile?

Thanks for every useful answer.

What I have tried:

I have tried

lvl1 = ConfigurationManager.AppSettings.Get("lvl1");



and

using System;
using System.Configuration;
using System.Collections.Specialized;

namespace ConConfig
{
    class Program
    {
        static void Main(string[] args)
        {
            string sAttr;

            // Read a particular key from the config file            
            sAttr = ConfigurationManager.AppSettings.Get("Key0");
            Console.WriteLine("The value of Key0: " + sAttr);

            // Read all the keys from the config file
            NameValueCollection sAll;
            sAll = ConfigurationManager.AppSettings;

            foreach (string s in sAll.AllKeys)
                Console.WriteLine("Key: " + s + " Value: " + sAll.Get(s));
           Console.ReadLine();
       }
    }
}



但它没有用,我得到的错误代码是在当前上下文中无法使用configurationManager与CS0103。我也搜索了互联网,但我没有得到解决方案。



这个似乎也没有解决问题,因为我收到属性不在的错误当前的背景。


But it did not work and I got the error code that configurationManager is not availible in the current context with CS0103. I also searched the internet but I didn't get a solution.

This one also does not seem to solve the problem since I get the error that Properties is not in the current context.

using System;
using System.Configuration;
using System.Collections.Specialized;

namespace ConConfig
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Properties.Settings.Default.lvl2)
            {
                Console.WriteLine("Sorry but you didn't unlock level 2 yet");
            }
            else
            {
                Console.WriteLine("You entered level 2...");
            }

            Properties.Settings.Default.lvl2 = true;
            Properties.Settings.Default.Save();
        }
    }
}

最简单的方法是在VS中打开你的项目,并双击解决方案资源管理器窗格中的属性。

在打开的页面中,单击左侧的设置,您将看到一个空白的页面,除了*消息:单击消息。

在名称列中,输入lvl2,将类型设置为bool,将范围保留为用户,然后输入False 价值栏目。

保存并关闭页面。

在您的控制台应用程序中,您现在可以写道:

The simplest way is to open your project in VS, and double click on "Properties" in the Solution Explorer pane.
In the page that opens, Click "Settings" on the left hand side, and you will get a page that is blank except for a central message: Click the message.
In the "Name" column, put "lvl2", set "Type" to "bool", leave "Scope" as "User", and enter "False" in the "Value" column.
Save and close the page.
In your console app you can now write:
if (Properties.Settings.Default.lvl2)
    {
    Console.Writeline("Sorry but you didn't unlock level 2 yet");
    }
else
    {
    Console.Writeline("You entered level 2...");
    }



你可以保存一个新值:


And you can save a new value:

Properties.Settings.Default.lvl2 = true;
Properties.Settings.Default.Save();

设置文件位于app / Bin / Release或app / Bin / Debug文件夹中,是人类可读的XML。

The settings file is in your app/Bin/Release or app/Bin/Debug folder, and is human readable XML.