C#外部程序调用目录错误

时间:2016-07-11 00:50:42

标签: c# call external

我正在开发从IBM开发的ILOG。 ILOG程序可以通过cmd控制台运行,如下所示:

oplrun -p C:\Users\pc_copat\opl\santez\"workName"

当我将上面的代码写入cmd控制台屏幕时,程序正在运行,没有错误。但是,当我在c#中使用这些代码时,它不起作用。

`string komut = @"oplrun -p C:\Users\pc_copat\opl\santez\ " + '\u0022' + calismaAdi + '\u0022';`

        ProcessStartInfo startInfo = new ProcessStartInfo();
        //startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "cmd.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "/K "+komut;

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using-statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
            // Log error.
        }

我总是收到这个错误:

  

' oplrun'不被识别为内部或外部命令,可操作程序或批处理文件。

如何解决此问题?

1 个答案:

答案 0 :(得分:-1)

您可以使用StandardInput.WriteLine在cmd上执行命令,而不是传递参数...

    string komut = @"oplrun -p C:\Users\pc_copat\opl\santez\ " + '\u0022' + calismaAdi + '\u0022';

    ProcessStartInfo startInfo = new ProcessStartInfo();
    //startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "cmd.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    //startInfo.Arguments = "/K "+komut;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardInput = true;
    string currentFolderString = AppDomain.CurrentDomain.BaseDirectory; // Not sure if this will work but it should be your directory containing the oplrun.exe
    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using-statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.StandardInput.WriteLine(string.format("cd \"{0}\"", currentFolderString)); // navigate to the oplrun.exe directory
            exeProcess.StandardInput.WriteLine(komut); // execute komut instead of as param
            exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }