简体   繁体   中英

C# Keeps forgetting my progress

using (StreamWriter sw = new StreamWriter(sName + ".txt", true)) 
{
       iDayNum++; //Adds a day on each time that case 3 is used.
       Console.Write("How many minutes have you been exercising? ");
       iExcerMinutes = Convert.ToInt32(Console.ReadLine()); 
       Console.WriteLine();

The code inside here works fine but when I reopen the program and ask the program what daynumber I am on it resets, what am I missing?

Hopefully this will help get you started - one technique is as follows.

Right-click your project, select properties/settings and create a user-scope setting.

I just created one called 'RunCount' of type int with value zero in a console application - I set the access modifier to 'public', as it happens.

The following code persists the run count incrementing it as it goes.

class Program
{
    static void Main(string[] args)
    { 
        Settings.Default.RunCount += 1;
        Settings.Default.Save();

        Console.WriteLine(String.Format("This is run number {0} for this user", Settings.Default.RunCount));  

        Console.WriteLine("Press a key to continue");
        Console.ReadKey();  
    }
}

If you were to run this example, then have a look in c:\\users\\"yourname"\\Appdata\\Local\\"yourprogramname" you will find a .config file containing :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <yourprogramname.Properties.Settings>
            <setting name="RunCount" serializeAs="String">
                <value>1</value>
            </setting>
        </yourprogramname.Properties.Settings>
    </userSettings>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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