简体   繁体   中英

c# load program settings after reboot computer

I have a problem with loading the settings to my program. After a system restart the program starts up automatically, but doesn't load the settings as it should. It does however load them when I run the application manually.

Starting the program with the system

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("Monitor", BaseDir+"\\Monitor.exe");

Class responsible for loading settings

class MySettings : AppSettings<MySettings>
{
    public string filePath = null;
    public string interval = "0";
}
public class AppSettings<T> where T : new()
{
    private const string DEFAULT_FILENAME = "settings.jsn";

    public void Save(string fileName = DEFAULT_FILENAME)
    {
        File.WriteAllText(fileName, (new JavaScriptSerializer()).Serialize(this));
    }

    public static void Save(T pSettings, string fileName = DEFAULT_FILENAME)
    {
        File.WriteAllText(fileName, (new JavaScriptSerializer()).Serialize(pSettings));
    }

    public static T Load(string fileName = DEFAULT_FILENAME)
    {
        T t = new T();
        if (File.Exists(fileName))
            t = (new JavaScriptSerializer()).Deserialize<T>(File.ReadAllText(fileName));
        return t;
    }
}

I'm using the following code to load my settings

MySettings settings = MySettings.Load();
string inter = settings.interval;

Why does the settings only load after restarting the application?

The issue may occur if the application's working directory is different when it's run by the system than if You're running it manually.

To fix this You can specify the absolute path to the settings file. You can do this by replacing the line:

private const string DEFAULT_FILENAME = "settings.jsn";

with:

private const string DEFAULT_FILENAME = @"C:\MyApplicationFolder\settings.jsn";

or:

private static readonly string DEFAULT_FILENAME = Path.Combine(Application.StartupPath, "settings.jsn");

Please let me know if need more help with this.

You can also use other suggestions mentioned in the comments for Your question.

UPDATE

If You cannot use Application.StartupPath property for some reason, You can also use:

private static readonly string DEFAULT_FILENAME = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.jsn");

I also haven't noticed that You use this path as default value in Your methods. To make it work You can try to change WorkingDirectory as suggested by Patrick in the comments or just use null as default and assign default value in the method's body like this:

public void Save(string fileName = null)
{
    AssignDefaultSettingsFilePathIfEmpty(ref fileName);

    File.WriteAllText(fileName, (new JavaScriptSerializer()).Serialize(this));
}

private void AssignDefaultSettingsFilePathIfEmpty(ref string fileName)
{
    if (string.IsNullOrEmpty(fileName))
    {
        fileName = DEFAULT_FILENAME;
    }
}

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