0

テスター (.cmd) を起動するアプリケーションを作成しているので、リストボックスに入力されたテストを渡しています。このメソッドは、入力されたテストが 1 つある場合は問題なく機能しますが、2 つ以上ある場合はエラーが発生します。

「タイプ 'System.ComponentModel.Win32Exception' の未処理の例外が System.dll で発生しました追加情報: システムは指定されたファイルを見つけることができません」

と両方StartInfo.FilenamecurrentTestFromListbox[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;
            }
        }
    }
}

ありがとう!-ジョエル

4

1 に答える 1

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.");
        }
    }
}
于 2013-06-28T14:23:39.640 に答える