私は現在、すでに構築されている既存のコンソールアプリを操作するアプリケーションを作成しています。現在、既存のアプリケーションを起動してから、コンソールに書き込んで出力を受け取ることができます。ただし、基本的にコンソールアプリをバックグラウンドで実行し続け、アプリを開いたままにして、ウィンドウに新しいコマンドを書き込んで詳細情報を受け取る準備をするために、アプリが必要です。以下は私が使用している現在のコードです。起動時にこのコードを呼び出してコンソールアプリケーションを起動する方法があるかどうか疑問に思っています。
コード:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string ApplicationPath = "python";
string ApplicationArguments = "Console/dummy.py";
string returnValue;
//Process PyObj = new Process();
ProcessStartInfo PyObjStartInfo = new ProcessStartInfo();
PyObjStartInfo.FileName = ApplicationPath;
PyObjStartInfo.Arguments = ApplicationArguments;
PyObjStartInfo.RedirectStandardInput = true;
PyObjStartInfo.RedirectStandardOutput = true;
PyObjStartInfo.UseShellExecute = false;
//PyObjStartInfo.CreateNoWindow = true;
//PyObj.StartInfo = PyObjStartInfo;
Thread.Sleep(5000);
using (Process process = Process.Start(PyObjStartInfo))
{
StreamWriter sw = process.StandardInput;
StreamReader sr = process.StandardOutput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("auth");
}
sw.Close();
sw.Close();
returnValue = sr.ReadToEnd();
MessageBox.Show(returnValue.ToString());
}
//Thread.Sleep(5000);
//PyObj.WaitForExit();
//PyObj.Close();
}
ご覧のとおり、これは現在ボタンクリックを利用していますが、アプリケーションが起動したらすぐにコードを実行したいと思います。次に、コンソールアプリを実行し、メモリ内に置いて、操作できるようにします。C#.netでこれを行う方法はありますか?
参考のために。私が呼び出しているコンソールアプリケーションは空白で、とりあえずダミーの答えを返します。これが以下のPythonコードです。
Pythonコード:
import os, pprint
def main():
keepGoing = True
while keepGoing:
response = menu()
if response == "0":
keepGoing = False
elif response == "auth":
print StartAuthProcess()
elif response == "verify":
print VerifyKey(raw_input(""))
elif response == "get":
print Info()
else:
print "I don't know what you want to do..."
def menu():
'''
print "MENU"
print "0) Quit"
print "1) Start Autentication Process"
print "2) Verify Key"
print "3) Get Output"
return raw_input("What would you like to do? ")
'''
return raw_input();
def StartAuthProcess():
return 1;
def VerifyKey(key):
if(key):
return 1;
else:
return 0;
def Info():
info = "{dummy:stuff}";
return info;
main()