如何在Windows启动时运行C#应用程序?
我做了一个启动过程中启动的应用程序,下面的代码。
该进程在重新启动后在进程pipe理器工具上运行,但在屏幕上看不到应用程序。 当我从启动registry值打开相同的.exe文件时,程序运行完美。
// The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); // Add the value in the registry so that the application runs at startup rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
我能做些什么来解决它?
代码在这里(赢得forms的应用程序):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; namespace RunAtStartup { public partial class frmStartup : Form { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); public frmStartup() { InitializeComponent(); // Check to see the current state (running at startup or not) if (rkApp.GetValue("MyApp") == null) { // The value doesn't exist, the application is not set to run at startup chkRun.Checked = false; } else { // The value exists, the application is set to run at startup chkRun.Checked = true; } } private void btnOk_Click(object sender, EventArgs e) { if (chkRun.Checked) { // Add the value in the registry so that the application runs at startup rkApp.SetValue("MyApp", Application.ExecutablePath); } else { // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("MyApp", false); } } } }
试试这个代码
private void RegisterInStartup(bool isChecked) { RegistryKey registryKey = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (isChecked) { registryKey.SetValue("ApplicationName", Application.ExecutablePath); } else { registryKey.DeleteValue("ApplicationName"); } }
资料来源: http : //www.dotnetthoughts.net/2010/09/26/run-the-application-at-windows-startup/
您可以尝试将快捷方式复制到启动文件夹中,而不是将其添加到registry中。 您可以使用Environment.SpecialFolder.Startup
获取path。 自1.1版以来,这在所有.net框架中都可用。
另外,也许这个网站会对你有所帮助,它列出了很多不同的方式让你的应用程序自动启动。
public class StartUpManager { public static void AddApplicationToCurrentUserStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""); } } public static void AddApplicationToAllUserStartup() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""); } } public static void RemoveApplicationFromCurrentUserStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My ApplicationStartUpDemo", false); } } public static void RemoveApplicationFromAllUserStartup() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My ApplicationStartUpDemo", false); } } public static bool IsUserAdministrator() { //bool value to hold our return value bool isAdmin; try { //get the currently logged in user WindowsIdentity user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); } catch (UnauthorizedAccessException ex) { isAdmin = false; } catch (Exception ex) { isAdmin = false; } return isAdmin; } }
你可以在这里查看整篇文章
首先我尝试下面的代码,它不工作
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString());
然后,我用LocalMachine改变了CurrentUser,它工作
RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString());
我没有find任何上面的代码工作。 也许这是因为我的应用程序正在运行.NET 3.5。 我不知道。 下面的代码对我来说是完美的。 我从我的团队的高级.NET应用程序开发人员那里得到了这个。
(Microsoft.Win32.Registry.LocalMachine,@“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \”,“WordWatcher”,“\”“+ Application.ExecutablePath.ToString()+”\“”);
public bool Write(RegistryKey baseKey, string keyPath, string KeyName, object Value) { try { // Setting RegistryKey rk = baseKey; // I have to use CreateSubKey // (create or open it if already exits), // 'cause OpenSubKey open a subKey as read-only RegistryKey sk1 = rk.CreateSubKey(keyPath); // Save the value sk1.SetValue(KeyName.ToUpper(), Value); return true; } catch (Exception e) { // an error! MessageBox.Show(e.Message, "Writing registry " + KeyName.ToUpper()); return false; } }
称为“Startup Creator”的开源应用程序通过创build脚本来configurationWindows启动,同时提供易于使用的界面。 使用function强大的VBScript,它允许应用程序或服务以定时延迟间隔启动,始终以相同的顺序启动。 这些脚本会自动放置在启动文件夹中,并可以打开备份以允许将来进行修改。
对于WPF:( 其中lblInfo是一个标签,chkRun是一个checkbox)
this.Topmost只是为了让我的应用程序在其他窗口的顶部,您还需要添加一个using语句“ using Microsoft.Win32; ”, StartupWithWindows是我的应用程序的名称
public partial class MainWindow : Window { // The path to the key where Windows looks for startup applications RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); public MainWindow() { InitializeComponent(); if (this.IsFocused) { this.Topmost = true; } else { this.Topmost = false; } // Check to see the current state (running at startup or not) if (rkApp.GetValue("StartupWithWindows") == null) { // The value doesn't exist, the application is not set to run at startup, Check box chkRun.IsChecked = false; lblInfo.Content = "The application doesn't run at startup"; } else { // The value exists, the application is set to run at startup chkRun.IsChecked = true; lblInfo.Content = "The application runs at startup"; } //Run at startup //rkApp.SetValue("StartupWithWindows",System.Reflection.Assembly.GetExecutingAssembly().Location); // Remove the value from the registry so that the application doesn't start //rkApp.DeleteValue("StartupWithWindows", false); } private void btnConfirm_Click(object sender, RoutedEventArgs e) { if ((bool)chkRun.IsChecked) { // Add the value in the registry so that the application runs at startup rkApp.SetValue("StartupWithWindows", System.Reflection.Assembly.GetExecutingAssembly().Location); lblInfo.Content = "The application will run at startup"; } else { // Remove the value from the registry so that the application doesn't start rkApp.DeleteValue("StartupWithWindows", false); lblInfo.Content = "The application will not run at startup"; } } }
如果你不能设置你的应用程序自动启动,你可以尝试粘贴这个代码来显示
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
或删除我在应用程序中find的清单
确定这里是我的2美分:尝试与每个反斜杠传递path作为双反斜杠。 我发现有时调用WIN API要求。
我认为有一个特定的Win32 API调用,它将应用程序的path,并将其自动在registry中放在适当的位置,我已经在过去使用它,但我不记得function名称了。