すでに何かが書かれ、テストされ、その作業に値するものである場合、ホイールを再発明するよりもこのツールを使用する方が適切です。 たとえば、コンソールユーティリティ
cpctest.exeを使用すると、グラフィカルシェルと同じアクションをすべて実行できます。また、Windowsの標準セットから他の多くのユーティリティを実行できます。 同様の機能を使用した開発、デバッグ、テストには貴重な時間がかかります。 なぜそれを燃やすのですか? 始めましょう。
まず、
MSDNにアクセスして、標準コマンドの構文を確認する必要があります。 その結果、コンソールアプリケーションの起動は、アプリケーションの名前とそのパラメーターで構成されていることがわかります。 アプリケーションは、
Processシステムクラスによってインスタンス化されます。 コンソールアプリケーションを起動するための関数のプロトタイプは次のようになります。
bool Execute(string commandName, IEnumerable<string> paramsList)
実行中のアプリケーションの実行結果を取得して処理する場合、適切なメソッドが必要です。
string GetResult (string commandName, IEnumerable<string> paramsList)
次に、MSDNに
戻り 、Javaのように例外を厳密に処理する場合は、ProcessStartInfo
Arguments 、
UseShellExecute 、
RedirectStandardOutput 、および
RedirectStandardErrorを確認します。 そのため、プロセスを初期化するには、コンソールアプリケーションの起動モードとプロセスを開始する方法を決定するプロパティが必要です。 私のクラスでは、Facadeパターンを使用しました。
public class CommandHelpers { public CommandHelpers() { Invisible = true; } public bool Invisible { get; set; } private Process CreateProcess(string commandName, IEnumerable<string> paramsList, bool output = false) { string paramString = paramsList.Aggregate<string, string>(null, (current, param) => current + " " + param); return new Process { StartInfo = { FileName = commandName, Arguments = paramString, UseShellExecute = output ? !output : !Invisible, RedirectStandardOutput = output } }; }
ping –t youwebsite.orgなど、実行中のアプリケーションは無期限に実行できることに注意してください。 それを実行するには、適切なメソッドが必要です。
public Task<bool> ExecuteAsync(string commandName, IEnumerable<string> paramsList)
ソースコード:
使用例: public class CspHelpers { private readonly CommandHelpers _cryptoconsole; private readonly string _command = @"c:\Program Files\Crypto Pro\CSP\csptest"; public CspHelpers() { _cryptoconsole = new CommandHelpers(); }
CommandHelpers.cs using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common.Security {
UPD:追加資料:
記事のインスピレーションの源:
ユーザー
vedmakaから:
toster.ru/q/7644コンソールアプリケーションのGUIラッパー:
en.jakeroid.com/gui-obertka-dlya-konsolnogo-prilozheniya-na-csharp.htmlユーザー
evgenylから:
habrahabr.ru/post/136766