2011-09-18 12 views
3

どのように私はコマンドプロンプトコマンドを実行するためにC#を使用するのですか?そのようなC#を使って簡単にシェルコマンドを実行するには?

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")を1つのコマンドプロンプトで実行する必要があります。ありがとう。

+2

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. 

ここでクラス(これは、PowerShellの機能のSystem.Automation nugetパッケージを使用しています)ですか? – 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

System.IOSystem.Netにある.Net同等品を使用する必要があります。

+2

あなたは間違ったアプローチをしています。 .Netフレームワークを使用してこれらのタスクを実行する方法を学ぶ必要があります。 MSDNのドキュメントを参照してください。 – SLaks

0

Hereソースコードを含むシェルコマンドを実行するためのソリューションがあります。これはstderrも考慮に入れています。

ただし、@SLaksとして指摘されています。.NET Framework(つまりSystem.IOSystem.Net)を使用して説明することができます。

その他の興味深い資源:

0

何をしようとしていますか?あなたは、スクリプトをやって見ても、.NET Frameworkを使用している場合は、Powershell

を見ているあなたはPowerhsellスクリプトでそのように言及するすべてのコマンドを使用することができます -

CD、ipconfigを、nslookupコマンドは、pingなどを
0

私はこの目的のために動的クラスのシェルを作成しました。あなたはそのようにそれを使用することができます。これはdownvotedなぜ

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; 
      } 
     } 

    } 
} 
関連する問題