[英]Windows Form Application with Console Application
我有一個控制台應用程序,它在啟動時會詢問SourcePath 。當我輸入Source Path時,會詢問DestinationPath ...當我進入DestinationPath時,它將開始執行
我的問題是通過Windows應用程序提供這些路徑,這意味着我需要創建一個窗口表單應用程序,該應用程序將在一定時間間隔后自動將這些參數提供給控制台應用程序
是否可以實現...如果是,請幫助...非常緊急...
哦。我嘗試了很多我無法粘貼的代碼,但我用來啟動應用程序的一些代碼是...
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = false;
psi.Arguments = input + ";" + output;
Process p = Process.Start(psi);
和
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = @"C:\Program Files\Wondershare\PPT2Flash SDK\ppt2flash.exe",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
if (process.Start())
{
Redirect(process.StandardError, text);
Redirect(process.StandardOutput, text);
MessageBox.Show(text);
}
private void Redirect(StreamReader input, string output)
{
new Thread(a =>{var buffer = new char[1];
while (input.Read(buffer, 0, 1) > 0)
{
output += new string(buffer);
};
}).Start();
}
但似乎沒有任何作用
您可以像這樣將參數添加到您的ProcessStartInfo中:
ProcessStartInfo psi = new ProcessStartInfo(@"C:\MyConsoleApp.exe",
@"C:\MyLocationAsFirstParamter C:\MyOtherLocationAsSecondParameter");
Process p = Process.Start(psi);
這將使用2個參數啟動控制台應用程序。 現在,在控制台應用程序中,
static void Main(string[] args)
字符串數組args包含參數,現在您要做的就是在應用啟動時獲取它們。
if (args == null || args.Length < 2)
{
//the arguments are not passed correctly, or not at all
}
else
{
try
{
yourFirstVariable = args[0];
yourSecondVariable = args[1];
}
catch(Exception e)
{
Console.WriteLine("Something went wrong with setting the variables.")
Console.WriteLine(e.Message);
}
}
這可能不是您所需要的確切代碼,但至少可以使您深入了解如何完成所需的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.