我正在编写一个启动测试程序(.cmd)的应用程序,所以我传入已经输入到列表框中的测试。如果输入了一个测试,此方法可以正常工作,但如果有2个或更多,则会给出错误:
“类型'System.ComponentModel.Win32Exception'的未处理异常 发生在System.dll中 附加信息:系统找不到指定的文件“
StartInfo.Filename
和currentTestFromListbox[i]
在调试器中看起来都是正确的。
任何人都知道我哪里出错了?
我很抱歉我的代码令人困惑 - 我只是一个初学者。
public void executeCommandFiles()
{
int i = 0;
int ii = 0;
int numberOfTests = listboxTestsToRun.Items.Count;
executeNextTest:
var CurrentTestFromListbox = listboxTestsToRun.Items.Cast<String>().ToArray();
string filenameMinusCMD = "error reassigning path value";
int fileExtPos = CurrentTestFromListbox[i].LastIndexOf(".");
if (fileExtPos >= 0)
{
filenameMinusCMD = CurrentTestFromListbox[i].Substring(0, fileExtPos);
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = @"pushd Y:\Tests\" + filenameMinusCMD + @"\" + CurrentTestFromListbox[i];
startInfo.WorkingDirectory = @"pushd Y:\Tests\" + filenameMinusCMD + @"\";
startInfo.FileName = CurrentTestFromListbox[i];
Process.Start(startInfo);
//Wait for program to load before selecting main tab
System.Threading.Thread.Sleep(10000);
//Select MainMenu tab by sending a left arrow keypress
SendKeys.Send("{LEFT}");
i++;
if (i < numberOfTests)
{
checkIfTestIsCurrentlyRunning:
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("nameOfProgramIAmTesting"))
{
System.Threading.Thread.Sleep(2000);
//if (ii > 150)
if (ii > 6) //test purposes only
{
MessageBox.Show("The current test (" + filenameMinusCMD + ") timed out at 5 minutes. The next test has been started.", "Test timed out",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
ii++;
goto checkIfTestIsCurrentlyRunning;
}
goto executeNextTest;
}
}
}
}
谢谢! -Joel
答案 0 :(得分:1)
这是您重新考虑的代码。睡觉/得到的确困扰着我。我无法真正测试它,但我认为它应该是一样的。如果它不起作用或者您有任何问题,请告诉我。
这假设您的列表框中包含以下内容:
testname.cmd
test2.cmd
test3.exe
lasttest.bat
这是我的尝试:
public void executeCommandFiles()
{
foreach (string test in listboxTestsToRun.Items)
{
//gets the directory name from given filename (filename without extension)
//assumes that only the last '.' is for the extension. test.1.cmd => test.1
string testName = test.Substring(0, test.LastIndexOf('.'));
//set up a FileInfo object so we can make sure the test exists gracefully.
FileInfo testFile = new FileInfo(@"Y:\Tests\" + testName + "\\" + test);
//check if it is a valid path
if (testFile.Exists)
{
ProcessStartInfo startInfo = new ProcessStartInfo(testFile.FullName);
//get the Process object so we can wait for it to finish.
Process currentTest = Process.Start(startInfo);
//wait 5 minutes then timeout (5m * 60s * 1000ms)
bool completed = currentTest.WaitForExit(300000);
if (!completed)
MessageBox.Show("test timed out");
//use this if you want to wait for the test to complete (indefinitely)
//currentTest.WaitForExit();
}
else
{
MessageBox.Show("Error: " + testFile.FullName + " was not found.");
}
}
}