0

我正在开发一个 Windows 应用程序,我想将此应用程序设置为 Windows 启动应用程序,为此我使用以下代码:-

代码

    public static void SetStartup(string AppName,
        bool enable)
    {
        try
        {
            string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);
            if (enable)
            {
                if (startupKey.GetValue(AppName) == null)
                {
                    startupKey.Close();
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                    startupKey.SetValue(AppName, Assembly.GetExecutingAssembly().Location + " /StartMinimized");
                    startupKey.Close();
                }
            }
            else
            {
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.DeleteValue(AppName, false);
                startupKey.Close();
            }
        }
        catch
        {

        }
    }

在应用程序加载时调用代码

SetStartup(Application.ExecutablePath, true);

这段代码运行良好。它将应用程序设置为启动应用程序。我检查在运行窗口中执行 msconfig 命令。它显示在启动选项卡中检查了此应用程序。但是当我重新启动系统时它不会启动应用程序。谁能告诉我问题是什么,我该如何解决这个问题。

4

3 回答 3

2

如果一切都表明它正在启动,那么我只能假设它的那部分是正确的,但应用程序由于某种原因无法启动。

当您启动应用程序运行时,它的工作目录设置为 C:\Windows\System32

我遇到了一些应用程序问题,这些应用程序可能正在其主目录中查找文件,例如配置文件,但无法找到它们。

通常无论如何都会找到以正常方式引用的文件,但是如果您在代码中手动指定路径,则可以使用:

string pathToDLL = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LibraryFile.dll");

使用 AppDomain.CurrentDomain.BaseDirectory 应该给出应用程序 exe 的路径,而不是工作目录。

这可能是问题的原因吗?

此外,我将假设 Vista 向上是操作系统,如果是这种情况,那么您的应用程序必须以提升的方式运行才能写入该注册表。因此,如果 UAC 关闭并且机器重新启动,那么您的应用程序(如果它在清单中设置为以 requireAdministrator 身份运行)将静默失败。

马丁

于 2013-06-06T07:05:30.623 回答
2

最后我得到了这个问题的答案。使用 StreamWriter 创建应用程序的 URL 链接,而不是在启动文件夹中创建 LNK。

创建快捷方式

    private void appShortcutToStartup()
    {
        string linkName ="MytestLink";
        string startDir = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        if (!System.IO.File.Exists(startDir + "\\" + linkName + ".url"))
        {
            using (StreamWriter writer = new StreamWriter(startDir + "\\" + linkName + ".url"))
            {
                string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
                writer.WriteLine("[InternetShortcut]");
                writer.WriteLine("URL=file:///" + app);
                writer.WriteLine("IconIndex=0");
                string icon = Application.StartupPath + "\\backup (3).ico";
                writer.WriteLine("IconFile=" + icon);
                writer.Flush();
            }
        }
    }

删除快捷方式

    private void delappShortcutFromStartup()
    {
        string linkName ="MytestLink";
        string startDir = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        if (System.IO.File.Exists(startDir + "\\" + linkName + ".url"))
        {
            System.IO.File.Delete(startDir + "\\" + linkName + ".url");
        }
    }

这段代码工作得很好。

于 2013-06-18T09:00:47.280 回答
0

我相信最简单的方法是按照以下步骤操作

1.) 构建您的应用程序

2.) 导航到您的调试文件夹

3)复制exe并将其放在您的启动位置

**C:\Documents and Settings\user\Start Menu\Programs\Startup**

            OR

只需将您的exe拖到开始菜单->程序->启动并将其粘贴到那里(即
释放鼠标按钮)

我想那会做你的工作

希望能帮助到你

于 2013-06-06T07:01:33.390 回答