简体   繁体   English

在启动时设置应用程序不起作用

[英]Setting an application on start-up not working

I'm developing an windows application and i want to set this application as windows start-up application for that i use this code:- 我正在开发Windows应用程序,并且我想将此应用程序设置为Windows启动应用程序,因为我使用以下代码:

Code

    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
        {

        }
    }

Calling code on application load 在应用程序加载时调用代码

SetStartup(Application.ExecutablePath, true);

And this code works fine.It sets application as a start-up application. 这段代码工作正常,将应用程序设置为启动应用程序。 I check that executing msconfig command in run window.It shows this application checked in start-up tab.But when i restarts the system it doesn't start application. 我在运行窗口中检查了是否正在执行msconfig命令。它显示该应用程序已在启动选项卡中选中。但是当我重新启动系统时,它不会启动应用程序。 Can any one tell me what is the problem and how can i solve that problem. 谁能告诉我问题是什么,我该如何解决。

If everything points to it being in startup then I can only assume that that part of it is correct, but the application is failing to start for some reason. 如果一切都表明它正在启动,那么我只能假定它的那一部分是正确的,但是由于某种原因应用程序无法启动。

When you start an application on run, it's working directory is set to C:\\Windows\\System32 在运行中启动应用程序时,其工作目录设置为C:\\ Windows \\ System32

I have had issues with applications that may be looking for files in its home directory such as config files but are unable to find them. 我遇到了一些应用程序问题,这些应用程序可能正在其主目录中查找文件,例如配置文件,但找不到它们。

Normally files referenced the normal way will be found anyway, but if you are manually specifying a path in your code you can use: 通常,无论如何都可以找到以正常方式引用的文件,但是如果您在代码中手动指定路径,则可以使用:

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

Using AppDomain.CurrentDomain.BaseDirectory should give the path of the your application exe, rather than the working directory. 使用AppDomain.CurrentDomain.BaseDirectory应该提供应用程序exe的路径,而不是工作目录。

Could this be the cause of the problem? 这可能是问题的原因吗?

Also, I'm going to assume Vista upwards is the OS, and if that's the case then your application would have to be running as elevated to write to that registry. 另外,我将假定Vista操作系统是OS,如果是这种情况,则您的应用程序必须以提升的权限运行才能写入该注册表。 So, if UAC is off and the machine is restarted then your application, if it's set in the manifest to run as requireAdministrator, would fail silently. 因此,如果UAC关闭并且计算机重新启动,则您的应用程序(如果已在清单中设置为以requireAdministrator的身份运行)将静默失败。

Martyn 马丁

Finally i got a answer for this problem.Use StreamWriter for creating a URL link of application instead of creating a LNK into start-up folder. 最后我得到了这个问题的答案。使用StreamWriter创建应用程序的URL链接,而不是在启动文件夹中创建LNK。

Create shortcut 建立捷径

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

Delete Shortcut 删除捷径

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

This code works very fine. 这段代码工作得很好。

I believe the most simplest way would be by following the below steps 我认为最简单的方法是按照以下步骤

1.) Build your application 1.)构建您的应用程序

2.) Navigate to your debug folder 2.)导航到调试文件夹

3) Copy the exe and place it at your Startup location 3)复制exe并将其放在您的启动位置

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

            OR

Simply drag your exe over start menu--> Program-->Startup and Paste it there (ie 只需将exe拖到开始菜单->程序->启动并将其粘贴到此处即可(即
releasing the mouse button) 释放鼠标按钮)

I guess that would do your work 我想那会做你的工作

Hope it helps 希望能帮助到你

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

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