如何在Windows启动时运行C#应用程序?

blo*_*dix 60 c# registry startup

我创建了一个在启动期间启动的应用程序,下面的代码如下.
重启后,该进程在进程管理器工具上运行,但我无法在屏幕上看到该应用程序.当我从启动注册表值打开相同的.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());
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

小智 55

代码在这里(win form app):

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @BoltClock真的很重要吗?此外,问题不是WPF问题,或者甚至与WPF有关,除了提到它之外.尽管如此,这只是无关紧要的信息.为了完全诚实,应删除WPF标记,以便清除与其相关的问题详细信息. (8认同)
  • 什么是`chkRun`? (3认同)
  • @Dinav Ahire:`chkRun`是(可检查的)表单控件,它显示和控制应用程序的start-with-windows状态. (2认同)

Anu*_*raj 36

试试这个代码

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://www.dotnetthoughts.net/2010/09/26/run-the-application-at-windows-startup/

  • 由于问题与WPF有关,请注意`Application.ExecutablePath`是`System.Windows.Forms`的一部分,并且会导致WPF项目中的`无法解析符号`.您可以使用`System.Reflection.Assembly.GetExecutingAssembly().Location`作为正确的替换. (15认同)
  • Assembly.GetExecutingAssembly()将获得当前运行代码的程序集.如果代码在另一个程序集上执行,它将无法获得正确的程序集.请改用Assembly.GetEntryAssembly(). (4认同)

Phi*_*Gan 17

您可以尝试将应用程序的快捷方式复制到启动文件夹中,而不是将内容添加到注册表中.你可以得到路径Environment.SpecialFolder.Startup.自1.1以来,这在所有.net框架中都可用.

或者,也许这个网站对您有所帮助,它列出了许多可以让应用程序自动启动的不同方法.


Ras*_*lik 9

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里查看整篇文章


mam*_*mal 5

简单

在代码中添加两部分:

1-添加命名空间

using Microsoft.Win32;
Run Code Online (Sandbox Code Playgroud)

2-将应用程序添加到注册表:

RegistryKey key=Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("your_app_name", Application.ExecutablePath);
Run Code Online (Sandbox Code Playgroud)

如果您想从注册表中删除应用程序

key.DeleteValue("your_app_name",false);
Run Code Online (Sandbox Code Playgroud)


Dav*_*ras -1

我认为有一个特定的 Win32 API 调用,它获取应用程序路径并自动将其放入注册表中的正确位置,我过去使用过它,但我不再记得函数名称了。