c#通过程序在启动时添加应用程序

use*_*293 5 c#

想要在Windows->所有程序 - > StartUp到c#程序下添加/删除应用程序.

将欣赏上述代码/方向.

问候Raju

Man*_*oor 8

您可以在每次启动Windows时运行它,只需使用下面的2行代码即可

    RegistryKey Key =  Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);                       
    Key.SetValue("AppName", System.Reflection.Assembly.GetEntryAssembly().Location);
Run Code Online (Sandbox Code Playgroud)

如果你真的需要创建一个启动快捷方式,这里是代码

  private void CreateShortcutInStartUP()
        {
            try
            {
                Assembly code = Assembly.GetExecutingAssembly();
                String company =  Application.CompanyName;
                String ApplicationName = Application.ProductName;

                if( company != "" && ApplicationName != "") 
                {
                    String DesktopPath= Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + ApplicationName + @".appref-ms";
                    String ShortcutName= Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\" + company + @"\" + ApplicationName + @".appref-ms";
                    if (System.IO.File.Exists(ShortcutName))
                        System.IO.File.Copy(ShortcutName, DesktopPath, true);

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我目前正在使用上面的代码,所以你可以复制粘贴.确保您有设置公司名称.