2011-07-11 55 views
1

確定這是有用的前一天,現在不是......我不記得改變任何東西,但我可以運行這個reg.exe命令與CMD下面的參數,它的工作原理罰款並創建輸出文件。但在VC#中運行它不創建文件test_output.txt ???Visual C#進程參數不起作用

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(); 
+0

* Vistual C#process *?這隻野獸是什麼?還沒有遇到它。 –

+0

@thanosazlin - 既然你已經解決了你的問題,你是不是覺得這個問題有迴應的衝動? – grapkulec

回答

3

您應該改用Registry類。

+0

這裏只是一些額外的信息。經過一些測試後,我發現在命令行和.NET過程中針對HKLM \ SOFTWARE運行reg.exe會產生2個不同的結果集。所以我認爲從進程中使用reg.exe不會產生真正無關緊要的預期輸出,因爲如您所述,使用Registry更好。 – mockobject

+0

太棒了!註冊表工作完美謝謝 – thanosazlin

2

您的>output.txt是對命令解釋程序(cmd.exe)的指令。這不會工作調用reg.exe。考慮改爲調用cmd.exe,或重定向stdout並將它自己寫入文件。看到這個SO answer link

當然,如果沒有令人信服的理由去註冊Reg.exe,則應該使用Registry類。

+0

偉大!註冊表工作完美謝謝 – thanosazlin