但是失败了,貌似Win7不给修改那个注册表,请问用代码如何才能实现?~

解决方案 »

  1.   

    win7下用程序修改注册表好像要使用administrator用户.你使用这个用户在来试试看.
      

  2.   

     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);
      

  3.   

    upup!问题还没解决,ls两位能讲清楚点吗?
      

  4.   

    用代码使程序在win7下开机自动运行,还有别的办法吗?
      

  5.   

    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   );   
      }
      

  6.   


    [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();
                            }
                    }               
    这个或许对你有帮助的
      

  7.   

    upup!问题还没解决,ls两位能讲清楚点吗?
      

  8.   

    谢谢各位,8楼给的代码使得问题差不多解决了。可是我是要用checkbox来控制开机是否自动运行而不是在Main函数里面执行,上面的代码应该如何改呢?改了好几次都没成功~能否再指点一下?
      

  9.   

    急,在线等...等待如何用checkbox控制开机自动运行问题!~~
      

  10.   

    win7中对于注册表、本地文件都限制的比较多,把
    UAC(用户帐户控制)关了试试
      

  11.   

    笑死我了,看到以前的自己开一个该程序的配置文件,某一项专门记录是否点击了checkbox的开机启动选择
    每次程序load的时候,读这个值,如果以前是选择了开机启动,则程序启动后把这个checkbox.check继续打上勾
    每次程序退出前(或者其他时机),判断checkbox.check是否变化(也可以程序中加入一个标识变量来表示),改变了的话,就按改变的结果运行8楼的程序让其开机启动或者不开机启动,并且将选择结果保存到配置文件中
      

  12.   

    直接把EXE拖到开始菜单的启动项里去不就行了。。
      

  13.   

    直接把EXE拖到开始菜单的启动项里去不就行了。。
      

  14.   

    WIN 7 下需要把UAC关掉,另外开机就运行,需要写个服务程序来唤醒你的程序;这样不需要桌面你程序就开始运行了
      

  15.   

    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();
    }
    }
    }
      

  16.   

    如果别的系统正常,只有win7不行,应该就是权限问题了。win7的权限管的很严,如果你在自己的电脑上用当然可以修改权限,但是如果要发布给客户使用,建议还是写服务或者在安装时或者初次运行时将快捷方式拷贝到启动项即可。
      

  17.   

    转帖文章一篇,请参考:http://mcgtts.javaeye.com/blog/751281