在Win7下做了个C#Winform程序,想要修改注册表使其开机自动运行! 但是失败了,貌似Win7不给修改那个注册表,请问用代码如何才能实现?~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 win7下用程序修改注册表好像要使用administrator用户.你使用这个用户在来试试看. string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "执行程序文件名称", true);//获得文件的当前路径 string dir = Directory.GetCurrentDirectory();//获取可执行文件的全部路径 string exeDir = dir + "WindowsApplication1.exe"; //获取Run键 RegistryKey key1=Registry.LocalMachine; RegistryKey key2=key1.CreateSubKey("SOFTWARE"); RegistryKey key3=key2.CreateSubKey("Microsoft"); RegistryKey key4=key3.CreateSubKey("Windows"); RegistryKey key5=key4.CreateSubKey("CurrentVersion"); RegistryKey key6=key5.CreateSubKey("Run"); //在Run键中写入一个新的键值 key6.SetValue("myForm",exeDir); key6.Close(); //如果要取消的话就将key6.SetValue("myForm",exeDir);改成 //key6.SetValue("myForm",false); upup!问题还没解决,ls两位能讲清楚点吗? 用代码使程序在win7下开机自动运行,还有别的办法吗? 1.写个批处理2.直接用代碼寫到註冊表裡,也可以手動添加. D:\\tractor.exe//可以是你的程序名何完整路徑就OK了 也可以手動拖到啟動裡面. RegistryKey hklm=Registry.LocalMachine; RegistryKey run=hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { run.SetValue("tractor.exe","D:\\tractor.exe"); MessageBox.Show(" 注册表添加成功!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information ); hklm.Close(); // button1_Click(this.button2,null); } catch(Exception my) { MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error ); } [STAThread] static void Main(string[] Args) { //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); //创建Windows用户主题 Application.EnableVisualStyles(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal( identity ); //判断当前登录用户是否为管理员 if( principal.IsInRole( System.Security.Principal.WindowsBuiltInRole.Administrator ) ) { //如果是管理员,则直接运行 Application.EnableVisualStyles(); Application.Run(new MainForm()); } else { //创建启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); //设置运行文件 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; //设置启动参数 startInfo.Arguments = String.Join( " ", Args ); //设置启动动作,确保以管理员身份运行 startInfo.Verb = "runas"; //如果不是管理员,则启动UAC System.Diagnostics.Process.Start( startInfo ); //退出 System.Windows.Forms.Application.Exit(); } } 这个或许对你有帮助的 upup!问题还没解决,ls两位能讲清楚点吗? 谢谢各位,8楼给的代码使得问题差不多解决了。可是我是要用checkbox来控制开机是否自动运行而不是在Main函数里面执行,上面的代码应该如何改呢?改了好几次都没成功~能否再指点一下? 急,在线等...等待如何用checkbox控制开机自动运行问题!~~ win7中对于注册表、本地文件都限制的比较多,把UAC(用户帐户控制)关了试试 笑死我了,看到以前的自己开一个该程序的配置文件,某一项专门记录是否点击了checkbox的开机启动选择每次程序load的时候,读这个值,如果以前是选择了开机启动,则程序启动后把这个checkbox.check继续打上勾每次程序退出前(或者其他时机),判断checkbox.check是否变化(也可以程序中加入一个标识变量来表示),改变了的话,就按改变的结果运行8楼的程序让其开机启动或者不开机启动,并且将选择结果保存到配置文件中 直接把EXE拖到开始菜单的启动项里去不就行了。。 直接把EXE拖到开始菜单的启动项里去不就行了。。 WIN 7 下需要把UAC关掉,另外开机就运行,需要写个服务程序来唤醒你的程序;这样不需要桌面你程序就开始运行了 if (LinkLabel1.Text.StartsWith("[点击")) { if (Interaction.MsgBox("为避免误操作,请您确认:" + Constants.vbCrLf + "您确定要加入启动项吗?" + Constants.vbCrLf + "加入启动项后,当Windows启动后本程序将自动启动!", Constants.vbYesNo + Constants.vbQuestion, "询问") == DialogResult.Yes) { try { Microsoft.Win32.RegistryKey KEY = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); KEY.SetValue("DS名言警句", Application.ExecutablePath); KEY.Close(); RegIsOK.Text = "操作成功"; RegIsOK.ForeColor = Color.Green; RegIsOK.Show(); } catch { RegIsOK.ForeColor = Color.Red; RegIsOK.Text = "操作失败"; RegIsOK.Show(); } }} else { if (Interaction.MsgBox("为避免误操作,请您确认:" + Constants.vbCrLf + "您确定要从启动项中删除DS名言警句吗?" + Constants.vbCrLf + "删除后,当Windows启动时本程序将不会自动启动!", Constants.vbYesNo + Constants.vbQuestion, "询问") == DialogResult.Yes) { try { Microsoft.Win32.RegistryKey KEY = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); KEY.DeleteValue("DS名言警句", false); KEY.Close(); RegIsOK.Text = "操作成功"; RegIsOK.ForeColor = Color.Green; RegIsOK.Show(); } catch { RegIsOK.ForeColor = Color.Red; RegIsOK.Text = "操作失败"; RegIsOK.Show(); } }} 如果别的系统正常,只有win7不行,应该就是权限问题了。win7的权限管的很严,如果你在自己的电脑上用当然可以修改权限,但是如果要发布给客户使用,建议还是写服务或者在安装时或者初次运行时将快捷方式拷贝到启动项即可。 转帖文章一篇,请参考:http://mcgtts.javaeye.com/blog/751281 UdpClient.Send的问题 c# txt下划线的问题 求 C# 图表控件 求助,求平均值 将Windows桌面扩展到该显示器 问一个关于程序打包问题。 请教一个winfrom下dll调试的问题,谢谢 如何禁止鼠标移动和点击呢? .Net源码中那些bin目录里面的*.DLL,*.VB文件有什么用处。 怎么把一个目录及其子目录装入TreeView中 谁帮我翻译下这段c++代码,转换成c#的 Image保存到Stream后的问题
System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "执行程序文件名称", true);//获得文件的当前路径
string dir = Directory.GetCurrentDirectory();//获取可执行文件的全部路径
string exeDir = dir + "WindowsApplication1.exe";
//获取Run键
RegistryKey key1=Registry.LocalMachine;
RegistryKey key2=key1.CreateSubKey("SOFTWARE");
RegistryKey key3=key2.CreateSubKey("Microsoft");
RegistryKey key4=key3.CreateSubKey("Windows");
RegistryKey key5=key4.CreateSubKey("CurrentVersion");
RegistryKey key6=key5.CreateSubKey("Run"); //在Run键中写入一个新的键值
key6.SetValue("myForm",exeDir);
key6.Close();
//如果要取消的话就将key6.SetValue("myForm",exeDir);改成
//key6.SetValue("myForm",false);
2.
直接用代碼寫到註冊表裡,也可以手動添加.
D:\\tractor.exe//可以是你的程序名何完整路徑就OK了
也可以手動拖到啟動裡面.
RegistryKey hklm=Registry.LocalMachine;
RegistryKey run=hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
run.SetValue("tractor.exe","D:\\tractor.exe");
MessageBox.Show(" 注册表添加成功!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
hklm.Close();
// button1_Click(this.button2,null);
}
catch(Exception my)
{
MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error );
}
[STAThread] static void Main(string[] Args)
{
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal( identity );
//判断当前登录用户是否为管理员
if( principal.IsInRole( System.Security.Principal.WindowsBuiltInRole.Administrator ) )
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join( " ", Args );
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start( startInfo );
//退出
System.Windows.Forms.Application.Exit();
}
}
这个或许对你有帮助的
UAC(用户帐户控制)关了试试
每次程序load的时候,读这个值,如果以前是选择了开机启动,则程序启动后把这个checkbox.check继续打上勾
每次程序退出前(或者其他时机),判断checkbox.check是否变化(也可以程序中加入一个标识变量来表示),改变了的话,就按改变的结果运行8楼的程序让其开机启动或者不开机启动,并且将选择结果保存到配置文件中
if (Interaction.MsgBox("为避免误操作,请您确认:" + Constants.vbCrLf + "您确定要加入启动项吗?" + Constants.vbCrLf + "加入启动项后,当Windows启动后本程序将自动启动!", Constants.vbYesNo + Constants.vbQuestion, "询问") == DialogResult.Yes) {
try {
Microsoft.Win32.RegistryKey KEY = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
KEY.SetValue("DS名言警句", Application.ExecutablePath);
KEY.Close();
RegIsOK.Text = "操作成功";
RegIsOK.ForeColor = Color.Green;
RegIsOK.Show();
} catch {
RegIsOK.ForeColor = Color.Red;
RegIsOK.Text = "操作失败";
RegIsOK.Show();
}
}
} else {
if (Interaction.MsgBox("为避免误操作,请您确认:" + Constants.vbCrLf + "您确定要从启动项中删除DS名言警句吗?" + Constants.vbCrLf + "删除后,当Windows启动时本程序将不会自动启动!", Constants.vbYesNo + Constants.vbQuestion, "询问") == DialogResult.Yes) {
try {
Microsoft.Win32.RegistryKey KEY = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
KEY.DeleteValue("DS名言警句", false);
KEY.Close();
RegIsOK.Text = "操作成功";
RegIsOK.ForeColor = Color.Green;
RegIsOK.Show();
} catch {
RegIsOK.ForeColor = Color.Red;
RegIsOK.Text = "操作失败";
RegIsOK.Show();
}
}
}