C# Exe进程仅在VS2010单元测试中有效,而不是在MVC应用程序中

C# Exe进程仅在VS2010单元测试中有效,而不是在MVC应用程序中,c#,asp.net-mvc,visual-studio-2010,unit-testing,process,C#,Asp.net Mvc,Visual Studio 2010,Unit Testing,Process,我有一个类处理exe进程的执行,这是一个控制台应用程序(用Fortran编写!)。运行此程序时,用户必须首先输入输入文件(必须与exe位于同一位置)和输出文件(尚不存在,但由exe生成)的名称 在我对Execute方法的单元测试中(见下文),一切正常。输出文件正确生成。但是,当我从MVC应用程序中点击此方法时,exe无法找到输入文件,即使它确实存在。我能想到的唯一解释是“TestResults”VS2010自动创建的文件夹在权限方面有所不同,或者单元测试过程有所不同 public voi

我有一个类处理exe进程的执行,这是一个控制台应用程序(用Fortran编写!)。运行此程序时,用户必须首先输入输入文件(必须与exe位于同一位置)和输出文件(尚不存在,但由exe生成)的名称

在我对Execute方法的单元测试中(见下文),一切正常。输出文件正确生成。但是,当我从MVC应用程序中点击此方法时,exe无法找到输入文件,即使它确实存在。我能想到的唯一解释是“TestResults”VS2010自动创建的文件夹在权限方面有所不同,或者单元测试过程有所不同

    public void Execute(string inputFileName, string outputFileName, int timeoutMilliseconds)
    {
        if (outputFileName == null)
            throw new ArgumentNullException("outputFileName");
        else if (inputFileName == null)
            throw new ArgumentNullException("inputFileName");

        if (outputFileName == string.Empty)
            throw new ArgumentException("outputFileName must not be an empty string");
        else if (inputFileName == string.Empty)
            throw new ArgumentException("inputFileName must not be an empty string");

        if (!File.Exists(ConfigurationManager.AppSettings["dataPath"]  + inputFileName))
            throw new FileNotFoundException("File not found.", inputFileName);            

        Process p = new Process();
        p.StartInfo = GetProcessStartInfo();
        p.Start();

        string output = "";

        // Read up to line where program asks for input file name.
        while (output == "")            
            output = p.StandardOutput.ReadLine();            

        // Write the input file name.
        p.StandardInput.WriteLine(inputFileName);

        // Read up to line where program asks for output file name.
        output = "";
        while (output == "")            
            output = p.StandardOutput.ReadLine();            

        // Write the output file name.
        p.StandardInput.WriteLine(outputFileName);

        if(!p.WaitForExit(timeoutMilliseconds))
            throw new ApplicationException("The process timed out waiting for a response.");            
    }

    private ProcessStartInfo GetProcessStartInfo()
    {
        var startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardInput = true;
        startInfo.FileName = _exeFileLocation;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;            
        return startInfo;
    }
编辑:

我发现,如果我尝试从C驱动器上的某个位置运行该进程,单元测试也会失败。在下面的代码中,如果我将“dataPath”设置为硬盘位置(例如“C:\DATALOCATION”),则该进程会失败,表示找不到输入文件。但是如果我离开“dataPath”在配置为空字符串时,单元测试会自动从TestResults文件夹的子目录中运行该进程,并且所有工作正常

    [TestMethod]
    public void ExeRunnerTest()
    {
        var studyData = // populate study data here
        var writer = new StudyDataFileWriter(studyData);

        // Write input
        string fileName = writer.WriteToFile();

        // Run 
        var exeRunner = new ExeRunner(ConfigurationManager.AppSettings["dataPath"] + "myProcess.exe");
        exeRunner.Execute(fileName, "outputFile", 10000);

        // Read output
        IStudyOutputFileReader reader = new StudyOutputFileReader(ConfigurationManager.AppSettings["dataPath"] + "outputFile");
        StudyOutput output = reader.Read();

        // Tests
        Assert.AreEqual(1, output.ConsumerSummary.ConsumerTypes.Count());
    }

使用绝对路径使代码正常工作。工作目录一定有问题。

我猜语言是C#,这就是我添加标记的原因。请始终在适用的情况下添加适当的语言标记。这使对某些主题不感兴趣的人更容易用这些标记筛选出问题。
.AppSettings[“数据路径”] +“MyPrime.exe”< /Cuff>将两个字符串连在一起。如果第一个字符串不以A结尾,则可能不构建所需的路径。请考虑使用<代码> Stry.IO .PATH。
。它可能与工作目录有关,因此传递绝对文件名似乎是明智的。Damien-使用绝对路径使代码工作(无论出于何种原因),所以我很高兴。除非您想将其作为答案发布,否则我不能将其标记为答案?