1

ok this was working the other day and now it's not... i don't remember changing anything, but i can run this reg.exe command with the arguments below from CMD and it works fine and creates the output file. but running it in VC# it does not create the file test_output.txt???

    System.Diagnostics.Process proc_cmd = new System.Diagnostics.Process();
        proc_cmd.StartInfo.FileName = @"c:\windows\system32\reg.exe";
        proc_cmd.StartInfo.Arguments = @"query ""HKLM\Software\test\test software"" /v BuildNumber >c:\test\test_output.txt";
        proc_cmd.StartInfo.CreateNoWindow = true;
        proc_cmd.StartInfo.RedirectStandardError = true;
        proc_cmd.StartInfo.RedirectStandardOutput = true;
        proc_cmd.StartInfo.RedirectStandardInput = true;
        proc_cmd.StartInfo.UseShellExecute = false;
        proc_cmd.Start();
    proc_cmd.Close();
Adrian Seeley
  • 2,262
  • 2
  • 29
  • 37
thanosazlin
  • 189
  • 1
  • 10

2 Answers2

3

You should use the Registry class instead.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Just some extra info here. After some testing I found that running reg.exe from against HKLM\SOFTWARE at commandline and from a .net process yields 2 different result sets. So I think that using reg.exe from a process will not yield the expected output which really is irrelevant since it is much better to use Registry as you mentioned. – mockobject Jul 11 '11 at 22:31
2

Your >output.txt is an instruction to the command interpreter (cmd.exe). That won't work calling reg.exe. Consider calling cmd.exe instead, or redirecting the stdout and writing it to the file yourself. See this SO answer link.

Of course, if there's no compelling reason to shell out to the Reg.exe, you should use the Registry class.

Community
  • 1
  • 1
agent-j
  • 27,335
  • 5
  • 52
  • 79