C# 无法从C执行已编译的matlab可执行文件#

C# 无法从C执行已编译的matlab可执行文件#,c#,matlab,.net-3.5,C#,Matlab,.net 3.5,对于我正在处理的一个模块,我收到了一个编译好的Matlab可执行文件(注意它是一个独立的.exe,不是.dll或类似的东西),我必须执行它来为我做一些分析工作 工作流程是创建一个输入文件(纯简单的.csv格式),执行.exe并解析Matlab可执行文件生成的输出文件(也是.csv) 我已经测试了输入文件生成和输出文件解析,如果我自己说的话,它们工作得很好。但是我在运行Matlab可执行文件时遇到了一些问题。我已经安装了正确的MCR,我可以双击可执行文件,它会按预期运行。但使用以下代码,可执行文件

对于我正在处理的一个模块,我收到了一个编译好的Matlab可执行文件(注意它是一个独立的.exe,不是.dll或类似的东西),我必须执行它来为我做一些分析工作

工作流程是创建一个输入文件(纯简单的.csv格式),执行.exe并解析Matlab可执行文件生成的输出文件(也是.csv)

我已经测试了输入文件生成和输出文件解析,如果我自己说的话,它们工作得很好。但是我在运行Matlab可执行文件时遇到了一些问题。我已经安装了正确的MCR,我可以双击可执行文件,它会按预期运行。但使用以下代码,可执行文件无法正确执行:

    var analyzer = new Process
    {
        StartInfo =
        {
            FileName = Path.Combine(WorkDirectory, "analyzer.exe"),
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true // These lines were added for debugging purposes
        }
    };
    analyzer.Start();
    punProcess.WaitForExit();
    string debuginfo = punProcess.StandardOutput.ReadToEnd();
    string debuginfo2 = punProcess.StandardError.ReadToEnd();
我从“debuginfo”中提取的文本如下:

     {Warning: Name is nonexistent or not a directory: C:\MATLAB\R2009b\toolbox\pun.} 
     {> In path at 110
        In addpath at 87
        In startup at 1} 
     {Warning: Name is nonexistent or not a directory:
     C:\MATLAB\R2009b\toolbox\pun\pun.} 
     {> In path at 110
        In addpath at 87
        In startup at 2} 
我从“debuginfo2”中提取的文本是:

这些问题是由于我的代码造成的吗?它们是由于通过C#使用它的上下文造成的吗?或者分析仪本身可能有问题?我无权访问analyzer可执行文件的源代码,因此无法调试该部分

发生的错误,可能是因为给出了警告,并且我错过了某种引用(可能是MCR?),当我双击它时(或者从cmd运行它,也像一个符咒一样工作),这种引用隐式可用

workdirectory将签出。我可以看到先前的C#代码在那里创建的输入文件,以及在那里复制的可执行文件。因此,问题不是由于在正确的位置准备正确的文件时出错造成的

干杯,
Xilconic

正如Dmitriy Reznik评论的那样,指定StartInfo的工作目录解决了我遇到的问题。应该是这样的:

var analyzer = new Process
{
    StartInfo =
    {
        FileName = Path.Combine(WorkDirectory, "analyzer.exe"),
        WorkingDirectory = WorkDirectory
        CreateNoWindow = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true // These lines were added for debugging purposes
    }
};
analyzer.Start();
punProcess.WaitForExit();

尝试将ProcessStartInfo.WorkingDirectory设置为.exe位置。
var analyzer = new Process
{
    StartInfo =
    {
        FileName = Path.Combine(WorkDirectory, "analyzer.exe"),
        WorkingDirectory = WorkDirectory
        CreateNoWindow = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true // These lines were added for debugging purposes
    }
};
analyzer.Start();
punProcess.WaitForExit();