Windows重新启动时自动启动

时间:2014-04-03 07:41:48

标签: c# windows startup autostart

我正在尝试用C sharp编写一个简单的程序,当Windows启动并继续工作时应该自动启动。我尝试了几种方法,虽然没有用。有什么建议?注意:我是初学者。

我试过了:

var thisPath = System.IO.Directory.GetCurrentDirectory();
                    RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                    string name = Path.GetFileName(Application.ExecutablePath);
                    if (regKey == null) // Key not existing, we need to create the key.
                    {
                       Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");

                    }

                    regKey.SetValue(name, (string)path, RegistryValueKind.String);

还有:

string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
            WshShell shell = new WshShell();
            string shortcutAddress = startupFolder + @"\Mylibraryxd.lnk";
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
            shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup"; // set the description of the shortcut
            shortcut.WorkingDirectory = Application.StartupPath; /* working directory */
            shortcut.TargetPath = Application.ExecutablePath; /* path of the executable */
            shortcut.Save(); // save the shortcut 

这些都不正常,你有什么想法吗?谢谢!!!

0 个答案:

没有答案