首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#外部程序调用目录错误

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

Stack Overflow用户
提问于 2016-07-11 00:50:41
回答 1查看 76关注 0票数 0

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

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

当我将上面的代码写到cmd控制台屏幕上时,程序没有出错。不过,当我在c#中使用这些代码时,如下所示,它不起作用。

代码语言:javascript
复制
`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”不被识别为内部或外部命令、可操作的程序或批处理文件。

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2016-07-11 01:14:47

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

代码语言:javascript
复制
    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.
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38297757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档