【问题标题】:Error while starting a WPF-Application on System-Startup在系统启动时启动 WPF 应用程序时出错
【发布时间】:2020-09-23 14:00:38
【问题描述】:

我有一个 WPF 应用程序,如果我启动我的计算机,我想自动启动它。

我有一个窗口,用户可以在其中配置应用程序的一些设置,其中一个可能的配置选项是一个复选框,它允许用户禁用或启用应用程序以在系统启动时自动启动。

这是我在注册表中设置或删除值的方式,具体取决于用户在复选框中的选择:

try
        {
            var currentAssembly = Assembly.GetExecutingAssembly();
            var rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

            if (settingsViewModel.AutostartEnabled)
            {
                rkApp.SetValue(currentAssembly.GetName().Name, currentAssembly.Location);
            }
            else
            {
                rkApp.DeleteValue(currentAssembly.GetName().Name, false);
            }
        }
        catch (Exception exception)
        {
        }

我的问题是,即使应用程序已注册并且也可以在任务管理器的自动启动部分中看到,但每次重新启动计算机以检查应用程序是否已启动时,我都会收到以下错误:

“您正在尝试打开应用程序扩展 (.dll) 类型的文件”

那我做错了什么?有什么办法可以避免或修复此错误?我已经尝试将应用程序清单文件添加到我的项目中,以始终以管理员身份启动我的应用程序。但这似乎也不起作用。

我将不胜感激。

【问题讨论】:

  • currentAssembly.Location 的值是多少?
  • currentAssembly.Location 具有以下值:"C:\\Users\\Username\\source\\repos\\AppName\\AppName\\AppNameCore\\bin\\Debug\\netcoreapp3. 1\\AppName.dll"

标签: c# wpf .net-core visual-studio-2019


【解决方案1】:

尝试使用System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 而不是currentAssembly.Location

这应该为您提供正在运行的可执行文件的路径。 Assembly.GetEntryAssembly 没有。

【讨论】:

  • 非常感谢!这给了我一直在寻找的道路。
猜你喜欢
  • 2014-02-27
  • 2017-08-14
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2013-08-09
相关资源
最近更新 更多