简体   繁体   English

系统启动时未加载C#用户定义的应用程序设置

[英]C# user-defined application settings are not loaded on system startup

I have a C# WindowsForms application that uses Properties.Settings.Default to store application and user settings. 我有一个使用Properties.Settings.Default来存储应用程序和用户设置的C#WindowsForms应用程序。 Reading and writing custom user values works correct. 读取和写入自定义用户值的工作正常。

Application is set to automatically run on system startup or user sign on. 应用程序设置为在系统启动或用户登录时自动运行。 Here's the problem: when the app starts on system startup it cant load any user-specific settings. 问题出在这里:当应用在系统启动时启动时,它无法加载任何用户特定的设置。 They're all set to their default values. 它们都设置为其默认值。 When i close the app and restart it - all user settings are back. 当我关闭应用程序并重新启动时-所有用户设置都恢复了。

After debugging and logging i narrowed down the issue: on normal start application's working directory is set to the directory it was installed into. 经过调试和记录后,我缩小了范围:在正常启动应用程序的工作目录中将其设置为安装目录。 On system start (or user sign in) working directory is set to C:\\system32 (or similar). 在系统启动(或用户登录)时,工作目录设置为C:\\system32 (或类似名称)。 That's when user settings are not loaded. 那时没有加载用户设置。

Is this a proper behavior ? 这是适当的行为吗? If so, how would i correctly load (or reload) user settings? 如果是这样,我将如何正确加载(或重新加载)用户设置?

NOTE: My application is deployed with ClickOnce so i can't really control where the settings file is stored. 注意:我的应用程序是与ClickOnce一起部署的,因此我无法真正控制设置文件的存储位置。 Also, i don't have any issues persisting the settings between the version upgrades. 另外,我在版本升级之间保留设置没有任何问题。

Found a solution, although i'm not sure how "correct" it is. 找到了解决方案,尽管我不确定它的“正确性”。

In my application when user clicks on "Start on boot" checkbox option, i was writing the following into the registry: 在我的应用程序中,当用户单击“启动时启动”复选框选项时,我正在将以下内容写入注册表:

string keyname = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
RegistryKey key = Registry.CurrentUser.OpenSubKey(keyname, true);
key.SetValue("MyApp", Application.ExecutablePath.ToString());

The issue was resolved when i changed the app path to: 当我将应用程序路径更改为时,该问题已解决:

var startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs)
  + @"\Published\App.appref-ms";
key.SetValue("MyApp", startPath);

I think it has something to do with how ClickOnce apps are typically launched. 我认为这与ClickOnce应用程序的典型启动方式有关。 I start the app using the desktop shortcut, not directly running exe file. 我使用桌面快捷方式而不是直接运行exe文件来启动应用程序。

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

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