2011-09-18 25 views
3

我該如何使用c#來運行命令提示符命令? 可以說,我想在一個序列中運行以下命令:如何使用c#輕鬆運行shell命令?

cd F:/File/File2/...FileX/ 
ipconfig 
ping google.com 

或類似的東西...有人可以使這個方法:

void runCommands(String[] commands) 
{ 
    //method guts... 
} 

這樣,你的輸入是一系列字符串命令(即「ipconfig」,「ping 192.168.192.168」,「ping google.com」,「nslookup facebook.com」),它們應該在它們放入數組的特定順序中在單個命令提示符下執行。謝謝。

+2

爲什麼這個downvoted? – Tigran

+0

洛爾然後upvote它! lmao ... –

回答

1

,應該在一個命令提示在特定 序列,其中它們被放置在陣列

中執行

您可以在bat文件中編寫命令序列並按以下方式運行。

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "YOURBATCHFILE.bat"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

Reference

+0

非常感謝。 –

1

您應該使用.Net等價物,它可以在System.IOSystem.Net中找到。

+2

你採取了錯誤的做法。您應該學習如何使用.Net框架執行這些任務;請參閱MSDN上的文檔。 – SLaks

0

你到底想幹什麼?如果你正在尋找做腳本,還可以使用.NET Framework中,看看Powershell

您可以使用所有您在Powerhsell腳本提到這樣的命令 - CD,IPCONFIG,NSLOOKUP,中國平安等

0

我爲此寫了一個動態類Shell。您可以使用它像這樣:

var shell = new Shell(); 

shell.Notepad("readme.txt"); // for "notepad.exe readme.txt" 
shell.Git("push", "http://myserver.com"); // for "git push http://myserver.com" 
shell.Ps("ls"); // for executing "ls" in powershell; 
shell.Instance; // The powershell instance of this shell. 

這裏的類(它使用System.Automation NuGet包的PowerShell的功能):

using System; 
using System.Collections.Generic; 
using System.Dynamic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Diagnostics; 
using System.Management.Automation; 

namespace System.Diagnostics { 

    public class Shell : System.Dynamic.DynamicObject { 

     public Shell(): base() { } 
     public Shell(bool window) { Window = window; } 

     static string[] ScriptExtensions = new string[] { ".exe", ".cmd", ".bat", ".ps1", ".csx", ".vbs" }; 

     public string Path { get { return Environment.GetEnvironmentVariable("PATH"); } set { Environment.SetEnvironmentVariable("PATH", value); } } 

     PowerShell pshell = null; 

     public PowerShell Instance { get { return pshell ?? pshell = PowerShell.Create(); } } 

     public bool Window { get; set; } 

     public ICollection<PSObject> Ps(string cmd) { 
      Instance.AddScript(cmd); 
      return Instance.Invoke(); 
     } 

     public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { 

      var exe = Path.Split(';').SelectMany(p => ScriptExtensions.Select(ext => binder.Name + ext)).FirstOrDefault(p => File.Exists(p)); 

      if (exe == null) exe = binder.Name + ".exe"; 

      var info = new ProcessStartInfo(exe); 
      var sb = new StringBuilder(); 
      foreach (var arg in args) { 
       var t = arg.ToString(); 
       if (sb.Length > 0) sb.Append(' '); 
       if (t.Contains(' ')) { sb.Append('"'); sb.Append(t); sb.Append('"'); } else sb.Append(t); 
      } 
      info.Arguments = sb.ToString(); 
      info.CreateNoWindow = !Window; 
      info.UseShellExecute = false; 
      info.WindowStyle = Window ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden; 
      try { 
       var p = Process.Start(info); 
       p.WaitForExit(); 
       result = p.ExitCode; 
       return true; 
      } catch { 
       result = null; 
       return false; 
      } 
     } 

    } 
}