简体   繁体   English

重新启动计算机后c#加载程序设置

[英]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 UPDATE

If You cannot use Application.StartupPath property for some reason, You can also use: 如果由于某种原因无法使用Application.StartupPath属性,则还可以使用:

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: 要使其正常工作,您可以尝试按照Patrick在注释中的建议更改WorkingDirectory ,或仅使用null作为默认值,并在方法的主体中分配默认值,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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