2010-04-26 75 views
1

通过我的桌面应用程序的安装程序(C#代码),我想执行一个外部进程。我这样做使用下面的代码。从System.Diagnostics.Process.Start()方法启动的进程访问注册表项

  ProcessStartInfo sinfo = new ProcessStartInfo(); 
      sinfo.FileName = filePath; 
      sinfo.WorkingDirectory = workingDir; 
      sinfo.UseShellExecute = false; 
      using (Process proc = Process.Start(sinfo)) 
      { 
       proc.WaitForExit(); 
       int code = proc.ExitCode; 
      } 

我的外部进程基本上是试图读取一个注册表项并用它来处理(它只读,不写入reg)。

虽然我确认注册表项值已被安装程序更新(我可以通过注册表手动读取注册表项),但外部进程无法读取这些值。它试图如下:

   using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(regKey)) 
       { 
        return (string)rk.GetValue(subKey); 
       } 

当我把一个断点在上面的纹路,我看到RK其内部具有无子项。我在Win7环境下做了所有这些(不知道UAC是否引起麻烦)

现在,当我手动直接运行外部应用程序(即双击应用程序的exe)时,我能够读取注册条目容易。好像我在开始这个过程时做错了什么。任何人有什么想法是什么?

感谢, 卡皮尔

回答

0

你可能有一个访问问题。

尝试填写sinfo.username和sinfo.password属性,并确保有管理访问权限的用户启动过程

ProcessStartInfo sinfo = new ProcessStartInfo(); 
     sinfo.FileName = filePath; 
     sinfo.WorkingDirectory = workingDir; 
     sinfo.UseShellExecute = false; 
     sinfo.UserName = "username1"; 
     SecureString password = new SecureString(); 
     foreach (char c in "password1".ToCharArray()) { 
      password.AppendChar(c); 
     } 
     sinfo.Password = password; 
     using (Process proc = Process.Start(sinfo)) 
     { 
      proc.WaitForExit(); 
      int code = proc.ExitCode; 
     } 
+0

感谢您的回答马丁。但是我怎么填写用户名和密码属性。有没有任何Windows API获取当前用户的用户名和密码?我非常怀疑。 – Kapil 2010-04-30 22:35:25