2015-09-03 109 views
0

多個命令我想通過運行C#應用程序的幾個命令狀運行從C#應用程序

以前我有一個批處理文件,我跑它使用C#,但現在幾個命令需要輸入。但如何做到這一點?

我試圖

Process cmdprocess = new Process(); 
     ProcessStartInfo startinfo = new ProcessStartInfo(); 
     Environment.SetEnvironmentVariable("filename", FileName); 
     startinfo.FileName = @"C:\Users\cnandy\Desktop\St\2nd Sep\New_CN\New folder\Encrypt web.config_RSAWebFarm\Decrypt Connection String.bat"; 

     startinfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startinfo.CreateNoWindow = true; 
     startinfo.RedirectStandardInput = true; 
     startinfo.RedirectStandardOutput = true; 
     startinfo.UseShellExecute = false; 
     cmdprocess.StartInfo = startinfo; 
     cmdprocess.Start(); 

而在批處理文件

cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 

aspnet_regiis -pi "CustomKeys2" "C:\Users\cnandy\Desktop\Encryption_keys\CustomEncryptionKeys.xml" 

aspnet_regiis -pa "CustomKeys2" "NT AUTHORITY\NETWORK SERVICE" 

aspnet_regiis -pdf "connectionStrings" %filename% 

但是有效,他們沒有得到運行在所有執行。如何實現同樣的地方,對於最後的命令我可以接受輸入而不是硬編碼

"C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb" 

+0

的可能重複:http://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net – Kaitlyn

+0

命令參數可能不正確。嘗試&&而不是單個&。另外,考慮PowerShell作爲替代方案。 – Ryan

+0

應該指出的是,如果您在字符串本身之前添加了「@」,則不需要所有這些額外的反斜槓,例如:@「此處的命令爲C:\ Test」 – Kaitlyn

回答

3

試試這個:

Process p = new Process() 
{ 
    StartInfo = new ProcessStartInfo("cmd.exe") 
    { 
     RedirectStandardInput = true, 
     RedirectStandardOutput = true, 
     UseShellExecute = false, 
     CreateNoWindow = true 
    } 
}; 

p.Start(); 

using (StreamWriter sw = p.StandardInput) 
{ 
    sw.WriteLine("First command here"); 
    sw.WriteLine("Second command here"); 
} 

p.StandardInput.WriteLine("exit"); 

或者,嘗試這種更直接的方式(這也實現您所要求的最後一件事):

string strEntry = ""; // Let the user assign to this string, for example like "C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb" 

Process p = new Process() 
{ 
    StartInfo = new ProcessStartInfo("cmd.exe") 
    { 
     RedirectStandardInput = true, 
     RedirectStandardOutput = true, 
     UseShellExecute = false, 
     CreateNoWindow = true 
    } 
}; 

p.Start(); 

p.StandardInput.WriteLine("cd C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319"); 
p.StandardInput.WriteLine("aspnet_regiis -pi \"CustomKeys2\" \"C:\\Users\\cnandy\\Desktop\\Encryption_keys\\CustomEncryptionKeys.xml\""); 
p.StandardInput.WriteLine("aspnet_regiis -pa \"CustomKeys2\" \"NT AUTHORITY\\NETWORK SERVICE\""); 
p.StandardInput.WriteLine("aspnet_regiis -pdf \"connectionStrings\" " + strEntry); 

p.StandardInput.WriteLine("exit"); //or even p.Close(); 

如果使用第二種方法,建議讓用戶在文本框中輸入路徑,然後從文本框的Text屬性中獲取字符串,其中所有必要的額外反斜槓將自動添加。

應該提到的是,沒有cmd會顯示運行您的批處理文件或命令。

+0

感謝但 p。 StandardInput.WriteLine(@「aspnet_regiis -pi」CustomKeys2「」C:\ Users \ cnandy \ Desktop \ Encryption_keys \ CustomEncryptionKeys.xml「」); p.StandardInput.WriteLine(@「aspnet_regiis -pa」CustomKeys2「」NT AUTHORITY \ NETWORK SERVICE「」); p.StandardInput.WriteLine(@「aspnet_regiis -pdf」connectionStrings「」strEntry);這些行引起我編譯時錯誤,說無法識別的轉義序列。 – StrugglingCoder

+0

這可能是由於反斜槓造成的,您可以嘗試使用您想要傳遞的命令的原始格式,而不是使用「@」方法。 – Kaitlyn

+0

@ user3655102更新了我的答案,以便正確使用反斜槓,從而避免了此問題。 – Kaitlyn

1

使用Process對象啓動批處理文件。您可以使用環境變量在進程之間傳遞值。在這種情況下,從C#到bat文件,您可以使用環境變量傳遞值。

C#

Environment.SetEnvironmentVariable("searchString","*.txt") 

在bat文件,你可以訪問該值作爲像下面

dir %searchString% 

從C#

System.Diagnostics.Process.Start("path\commands.bat"); 

示例代碼啓動記事本啓動bat文件從C#與批處理文件和變量

System.Environment.SetEnvironmentVariable("fileName", "test.txt"); 
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/commands.bat"); 
Console.ReadLine(); 

.bat文件

@echo "staring note pad" 
notepad %fileName% 
+0

謝謝..我相應地修改了我的問題。但我仍然無法執行它們。你可以看一下嗎? – StrugglingCoder

+0

我已經添加了示例代碼,我已經過測試並且工作正常。我不想在我的服務器上運行「aspnet_regiis」。所以用記事本。 – CreativeManix

1

,您仍然可以編寫一個批處理文件,你習慣了。只有改變它的需求就是接受變量。

喜歡的東西

CallYourBatch.bat "MyFileName" 
在你的批處理文件

然後,您可以接受一個參數

SET fileName=%~1 
aspnet_regiis -pdf "connectionStrings" %fileName% 

同樣相同的功能,同時形成命令文本中使用,如果你都必須做的C#代碼的一部分。

也可以建議使用CALL命令來調用您的批處理文件?在相同的更多信息,在http://ss64.com/nt/call.html

+0

謝謝..我相應地修改了我的問題。但我仍然無法執行它們。你可以看一下嗎? – StrugglingCoder