Windows 启动后 WPF 应用程序未启动

发布于 2025-01-18 05:41:04 字数 1090 浏览 4 评论 0原文

我的应用程序在 Windows 启动后不会启动,该应用程序已在注册编辑器中使用我的应用程序 exe 的正确路径进行注册。

注册编辑器屏幕截图

这是我的代码,用于将我的应用程序注册到

       private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RUN", true);


        if (e.PropertyName == nameof(isStartOnStartup))
        {
            if (isStartOnStartup)
            {
                reg.SetValue("CommunicationHub", Process.GetCurrentProcess().MainModule.FileName);
                Settings.Default.Startup = true;
                Settings.Default.Save();

            }
            else
            {
                reg.DeleteValue("CommunicationHub");
                Settings.Default.Startup = false;
                Settings.Default.Save();
            }
        }


    }

我已检查 EventViewer 的 注册编辑器崩溃并添加了日志记录到应用程序根目录,两者都没有给我任何帮助。

我已经禁用了UAC,检查是否是管理员权限阻碍了应用程序的运行,仍然没有结果。

我感到失落,我希望有人能帮助我解决这个问题。

My Application will not launch after Windows startup, the application is registered in the register editor with the right path to my application exe.

Register Editor ScreenShot

here is my code to register my application to the register editor

       private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RUN", true);


        if (e.PropertyName == nameof(isStartOnStartup))
        {
            if (isStartOnStartup)
            {
                reg.SetValue("CommunicationHub", Process.GetCurrentProcess().MainModule.FileName);
                Settings.Default.Startup = true;
                Settings.Default.Save();

            }
            else
            {
                reg.DeleteValue("CommunicationHub");
                Settings.Default.Startup = false;
                Settings.Default.Save();
            }
        }


    }

I have checked EventViewer for crashes and have aswell added logging to the application root, both gave me nothing.

I have disabled UAC, to check if it was the administrator right that was hindering the application from running, still no result.

i feel lost, i hope someone can help me with the problem..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

a√萤火虫的光℡ 2025-01-25 05:41:05

找到了另一种方法,而不是将我的应用程序放在寄存器编辑器中,但是在任务计划中,它将在Windows启动上启动使用Admin权利的应用程序。但!只有我告诉任务调度程序将其启动为“登录“触发”而不是“在系统启动”上。但是,如果我组合td.triggers.addnew(tasktriggertype.boot);td.principal.userid =“ System”;>
它将使我的状态运行,但UI未显示。为什么?..如何解决此问题?

这是我的更新代码,我找到了一个可以完成计划的库。

库: https://github.com/dahall/taskscheduler

{
    TaskService ts = new TaskService();
    string program_path = Process.GetCurrentProcess().MainModule.FileName; 

    if (e.PropertyName == nameof(isStartOnStartup))
    {
        if (isStartOnStartup)
        {
            TaskDefinition td = ts.NewTask();

            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Triggers.AddNew(TaskTriggerType.Boot); // Not working  
            td.Principal.UserId = "SYSTEM"; // Run whether user is logged on or not. combined with line 132 runs the program but the ui is invicible
            //td.Triggers.AddNew(TaskTriggerType.Logon); // Runs and shows the UI perfectly fine
            td.Actions.Add(new ExecAction(program_path, null));
            ts.RootFolder.RegisterTaskDefinition("CommunicationHub", td);

            Settings.Default.Startup = true;
            Settings.Default.Save();
        }
        else
        {
            ts.RootFolder.DeleteTask("CommunicationHub", false);
            Settings.Default.Startup = false;
            Settings.Default.Save();
        }
    }
} 

Found another way to do this, instead of having my application in the register editor, but in Task Schedular, it will launch the application on windows startup with the admin rights. BUT! Only if I tell task scheduler to launch it as "at log on" trigger and not on "at system startup". But if I combine td.Triggers.AddNew(TaskTriggerType.Boot); and td.Principal.UserId = "SYSTEM";
it will give me the status running but the UI is not showing. Why?.. How do I fix this?

Here is my updated code, and I found a library to do the schedular thing.

library: https://github.com/dahall/TaskScheduler

{
    TaskService ts = new TaskService();
    string program_path = Process.GetCurrentProcess().MainModule.FileName; 

    if (e.PropertyName == nameof(isStartOnStartup))
    {
        if (isStartOnStartup)
        {
            TaskDefinition td = ts.NewTask();

            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Triggers.AddNew(TaskTriggerType.Boot); // Not working  
            td.Principal.UserId = "SYSTEM"; // Run whether user is logged on or not. combined with line 132 runs the program but the ui is invicible
            //td.Triggers.AddNew(TaskTriggerType.Logon); // Runs and shows the UI perfectly fine
            td.Actions.Add(new ExecAction(program_path, null));
            ts.RootFolder.RegisterTaskDefinition("CommunicationHub", td);

            Settings.Default.Startup = true;
            Settings.Default.Save();
        }
        else
        {
            ts.RootFolder.DeleteTask("CommunicationHub", false);
            Settings.Default.Startup = false;
            Settings.Default.Save();
        }
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文