Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I want to make a file that stores specific information that can be edited like:
(lvl2 = false)
C#
//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
}

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();
       }
    }
}

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();
        }
    }
}
Posted
Updated 14-Feb-17 1:22am
v3

1 solution

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();
The settings file is in your app/Bin/Release or app/Bin/Debug folder, and is human readable XML.
 
Share this answer
 
Comments
GrabiCraft 13-Feb-17 5:10am    
First of all thank you for the answer.
I did exactly as you explained but I get the error that properties is not availible again with the error code CS0103. Do you have any idea how I could solve this problem?
Thanks.
OriginalGriff 13-Feb-17 5:30am    
Check your case: C# is case sensitive and "properties" is not the same as "Properties"
If it isn't that, copy and paste your code.
GrabiCraft 13-Feb-17 5:37am    
Thank you I will definitely try that but since I am Unfortunately not at home I can't try it until tomorrow. Another response will come tomorrow.
Thanks!
GrabiCraft 13-Feb-17 5:40am    
Here is a picture of my Code:
https://plus.google.com/107961560865850716097/posts/MbUpLbMS4WL
OriginalGriff 13-Feb-17 6:08am    
A photo of your screen, taken on your phone, uploaded to google docs, and a link posted... oh dear...
On your keyboard is a "PrtSc" or "Print Screen" button. Hold down ALT and press that and it copies the current app screen to the clipboard as a bitmap. You get much better images that way...
But for code, highlight the text in VS and copy it, then paste it directly into a response - it's a lot more usable than an image of your screen.

Now, check you saved the Settings page, and that your property is visible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900