MSDN을 사용하여 커맨드 라인 도구의 래퍼를 작성했습니다.C#의 명령 행 도구 래퍼
이제 문제가 발생합니다. 인수를 사용하여 명령 줄에서 exe를 실행하면 오류없이 완벽하게 작동합니다.
그러나 래퍼에서 인수를 전달하려고하면 프로그램이 충돌합니다.
내가 논쟁을 적절히 통과하는지 알기를 원합니다. 내가 틀렸다면 누군가 지적 해주십시오. 이 이것은 내 메인 프로그램에서 인수를 전달하고있는 방법입니다 MSDN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace SPDB
{
/// <summary>
/// Class to run any external command line tool with arguments
/// </summary>
public class LaunchEXE
{
internal static string Run(string exeName, string argsLine, int timeoutSeconds)
{
StreamReader outputStream = StreamReader.Null;
string output = "";
bool success = false;
try
{
Process newProcess = new Process();
newProcess.StartInfo.FileName = exeName;
newProcess.StartInfo.Arguments = argsLine;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.CreateNoWindow = true; //The command line is supressed to keep the process in the background
newProcess.StartInfo.RedirectStandardOutput = true;
newProcess.Start();
if (0 == timeoutSeconds)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
newProcess.WaitForExit();
}
else
{
success = newProcess.WaitForExit(timeoutSeconds * 1000);
if (success)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
}
else
{
output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
}
}
}
catch (Exception e)
{
throw (new Exception("An error occurred running " + exeName + ".", e));
}
finally
{
outputStream.Close();
}
return "\t" + output;
}
}
}
에서 LaunchEXE의 클래스입니다 (Form1.cs를)
private void button1_Click(object sender, EventArgs e)
{
string output;
output = LaunchEXE.Run(@"C:\Program Files (x86)\MyFolder\MyConsole.exe", "/BACKUP C:\\MyBackupProfile.txt", 100);
System.Windows.Forms.MessageBox.Show(output);
}
는 명령 행 도구는 다음 명령을 받아 완벽하게 작동 :
C : \의 Program Files (x86) \ MyFolder에>MyConsole.exe/백업 C : \ MyBackupProfile.txt
"프로그램을 중단합니다"라고 말하면 예외, 오류 메시지 또는 다른 것을 얻습니까? – RQDQ
그냥 프로그램이 충돌, 그 명령 줄 도구에 대한 로그 파일을 가지고 있지만 나에 의해 만들어진되지 않습니다 ... 나는 내 잘못이며 그의 도구가 잘 작동하는 그 도구의 개발자로부터 답변을 가지고 있어요. 하지만이 도구의 로그 파일에서 얻은 것입니다 ... "System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다." 그러나 내 프로그램에서 예외 오류가 발생하지 않습니다 ... –
newpoint에서 중단 점. 시작(); 자체가 프로그램을 크래시 .. –