日期:2014-05-18  浏览次数:20882 次

C#写的程序如何在开机的时候自动运行,求解!
我用C#写了一个程序,如何能够开机的时候自动运行.急!!!

------解决方案--------------------
添加到启动项
 reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
------解决方案--------------------
分为两种
第一是简单的方式 直接在程序里面写进去 http://blog.csdn.net/fengyarongaa/article/details/6537548
C# code

//此方法把启动项加载到注册表中
//获得应用程序路径
string strAssName = Application.StartupPath + @"/" + Application.ProductName + @".exe";
//获得应用程序名
string ShortFileName = Application.ProductName;
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
if (rgkRun == null)
{
    rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
}
rgkRun.SetValue(ShortFileName, strAssName);

//此删除注册表中启动项
//获得应用程序名
string ShortFileName = Application.ProductName;
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
if (rgkRun == null)
{
    rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
}
rgkRun.DeleteValue(ShortFileName, false);

------解决方案--------------------
using System.Security.Permissions;
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify="HKEY_CURRENT_USER")]



Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue(name, value, valueKind)