Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置启动时启动的程序_C#_Windows - Fatal编程技术网

C# 如何设置启动时启动的程序

C# 如何设置启动时启动的程序,c#,windows,C#,Windows,我有一个带有复选框的小应用程序,如果用户希望应用程序以Windows启动,可以设置该复选框 我的问题是如何设置应用程序在启动时运行 注:我正在使用C#with.NET 2.0。几个选项,按优先顺序排列: 将其添加到当前用户的启动文件夹。这要求您的应用程序运行所需的权限最少,并为用户提供最多的控制和反馈。缺点是,下次他们在程序中查看该屏幕时,确定是否显示已选中的复选框有点困难 将其添加到HKey\U Current\U User\Software\Microsoft\Windows\Current

我有一个带有
复选框的小应用程序,如果用户希望应用程序以Windows启动,可以设置该复选框

我的问题是如何设置应用程序在启动时运行


注:我正在使用C#with.NET 2.0。

几个选项,按优先顺序排列:

  • 将其添加到当前用户的启动文件夹。这要求您的应用程序运行所需的权限最少,并为用户提供最多的控制和反馈。缺点是,下次他们在程序中查看该屏幕时,确定是否显示已选中的复选框有点困难
  • 将其添加到
    HKey\U Current\U User\Software\Microsoft\Windows\CurrentVersion\Run
    注册表项中。这里唯一的问题是它需要对注册表的写访问权限,而这并不总是可用的
  • 创建在用户登录时触发的计划任务
  • 将其添加到
    HKey\U Local\U Machine\Software\Microsoft\Windows\CurrentVersion\Run
    注册表项中。这里唯一的问题是它需要对注册表的写访问权限,而这并不总是可用的
  • 将其设置为windows服务。只有当你真的这么做了,并且你确信你想为计算机上的所有用户运行这个程序时,才可以这样做

  • 这个答案现在已经过时了。自从我写了这篇文章,Windows10发布了,它改变了“开始”菜单文件夹的工作方式。。。包括
    启动
    文件夹。我还不清楚在不引用Windows用于这些位置的内部数据库的情况下在该文件夹中添加或删除文件有多容易。

    您可以在“HKCU\Software\Microsoft\Windows\CurrentVersion\Run”中创建注册表项,但请注意,它在Vista上的工作方式可能不同。由于UAC,您的设置可能会“虚拟化”。

    如果应用程序设计为在Windows启动时启动(与用户登录时相反),则您唯一的选择是使用Windows服务。可以将应用程序作为服务编写,也可以编写一个仅用于启动应用程序的简单服务

    编写服务可能很棘手,并且可能会对您的特定情况施加不可接受的限制。一种常见的设计模式是前端/后端对,其中一个服务完成工作,另一个应用程序前端与服务通信以向用户显示信息


    另一方面,如果您只想在用户登录时启动应用程序,您可以使用Joel Coehoorn列出的方法1或2。

    感谢大家响应如此之快。 Joel,我使用了你的选项2,在当前用户的“Run”文件夹中添加了一个注册表项。 这是我为其他感兴趣的人使用的代码

        using Microsoft.Win32;
        private void SetStartup()
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    
            if (chkStartUp.Checked)
                rk.SetValue(AppName, Application.ExecutablePath);
            else
                rk.DeleteValue(AppName,false);            
    
        }
    

    可以使用Microsoft命名空间中的win32类执行此操作

    using Microsoft.Win32;
    
    using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
     {
                key.SetValue("aldwin", "\"" + Application.ExecutablePath + "\"");
     }
    
    //
    ///将应用程序添加到windows的启动
    /// 
    /// 
    /// 
    公共静态void AddStartup(字符串appName,字符串路径)
    {
    使用(RegistryKey key=Registry.CurrentUser.OpenSubKey)
    (“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,true))
    {
    key.SetValue(appName,“\”+路径+“\”);
    }
    }
    /// 
    ///从windows启动中删除应用程序
    /// 
    /// 
    公共静态void RemoveStartup(字符串appName)
    {
    使用(RegistryKey key=Registry.CurrentUser.OpenSubKey)
    (“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,true))
    {
    key.DeleteValue(appName,false);
    }
    }
    
    以下是将您的程序添加到Windows Vista 7、8、10的启动中的所有方法

    • 文件路径
    C:\Users\Bureau Briffault\AppData\Roaming\Microsoft\Windows\Start
    菜单\程序\启动
    (在任务管理器中可见,在当前计算机上运行 用户登录成功,无需管理员权限)

    C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start
    菜单\程序\启动
    (在任务管理器中可见,在所有用户上运行 登录成功,需要管理员权限)


    • 注册表路径
    HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\Run
    (从任务管理器可见,在当前用户登录成功时运行,否 管理员权限(必需)

    HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
    (在任务管理器中不可见,在当前用户登录成功时运行, 运行一次登录,无需管理员权限)

    HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
    (从任务管理器可见,在所有用户登录成功时运行,管理员 (所需特权)

    HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
    (在任务管理器中不可见,在所有用户登录成功时运行, 运行一次登录,需要管理员权限)


    • 任务计划程序
    Microsoft.Win32.Taskscheduler.dll
    (在任务管理器中不可见, 在windows引导下运行,以管理员身份运行,需要管理员权限)


    除了Xepher.com的答案外,Windows启动的文件夹路径也应按以下方式编码:

    var Startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
    

    这是一个非常简单的解决方案:

    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    key.SetValue("Your Application Name", Application.ExecutablePath);
    
    除去

    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    key.DeleteValue("Your Application Name", false);
    

    我发现给启动文件夹添加快捷方式对我来说是最简单的方法。我必须添加对“Windows脚本主机对象模型”和“Microsoft.CSharp”的引用,然后使用以下代码:

    IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
    string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\MyAppName.lnk";
    System.Reflection.Assembly curAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    
    IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "My App Name";
    shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
    shortcut.TargetPath = curAssembly.Location;
    shortcut.IconLocation = AppDomain.CurrentDomain.BaseDirectory + @"MyIconName.ico";
    shortcut.Save();
    

    添加一个应用程序,以便在Windows 10中启动时自动运行

    步骤1:选择Windows启动按钮并滚动找到启动时要运行的应用程序

    步骤2:右键单击应用程序,选择更多,然后选择打开文件位置。这将打开应用程序快捷方式的保存位置。如果没有打开fil的选项
    IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
    string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\MyAppName.lnk";
    System.Reflection.Assembly curAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    
    IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "My App Name";
    shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
    shortcut.TargetPath = curAssembly.Location;
    shortcut.IconLocation = AppDomain.CurrentDomain.BaseDirectory + @"MyIconName.ico";
    shortcut.Save();