繁体   English   中英

尝试在C#/ ASP.NET / IIS 7.5 / Win7环境中调用可执行的命令提示符

[英]Attempting to call command prompt execuable in a C# / ASP.NET / IIS 7.5 / Win7 Environment

在服务器上,我试图打开命令提示符并调用将文件转换为PDF的可执行文件。 为此,我正在使用PDFCreator开源程序。

在C#中,我使用以下代码进行调用:

    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;
    Process process = Process.Start(processStartInfo);

    process.StandardInput.WriteLine(@"cd c:\program files (x86)\pdfcreator");
    process.StandardInput.WriteLine(@"PDFCreator.exe /PF""c:\dwf\dwf.dwf""");

它运行没有错误,但没有任何结果。 此PDFCreator.exe的作用是调用另一个程序,即Autodesk Design Review,它将打开,使用PDF驱动程序将其打印为PDF,然后保存文件。 您看到的命令在我独立运行时工作正常。

通过搜索其他线程,似乎安全可能是我的问题。 因此,我进入了PDFCreator和Design Review文件夹/可执行文件,并授予了对NETWORK,NETWORK SERVICE,IIS_WPG,IIS_IUSRS和ASP.NET Machine帐户的完全访问权限(意识到这可能是一个安全线程,但是一旦我找出了问题)。 这没有帮助。

应该注意的是,我可以使用上面的第一个命令更改目录,然后在PDFCreator和Design Review文件夹中都创建一个“ test123”文件夹。 看来我正在靠近,有什么想法吗?

SteveCalPoly和Val Akkapeddi的评论非常有趣。
无论如何,我使用以下方法通过命令提示符运行可执行文件

    /// <summary>
    /// Executes a shell command synchronously.
    /// </summary>
    /// <param name="command">string command</param>
    /// <returns>string, as output of the command.</returns>
    public void ExecuteCommandSync(object command)
    {
        try
        {
            // create the ProcessStartInfo using "cmd" as the program to be run,
            // and "/c " as the parameters.
            // Incidentally, /c tells cmd that we want it to execute the command that follows,
            // and then exit.
            System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();
            // Get the output into a string
            string result = proc.StandardOutput.ReadToEnd();
            // Display the command output.
            Console.WriteLine(result);
        }
        catch (Exception objException)
        {
            // Log the exception
        }
    }
    /// <summary>
    /// Execute the command Asynchronously.
    /// </summary>
    /// <param name="command">string command.</param>
    public void ExecuteCommandAsync(string command)
    {
        try
        {
            //Asynchronously start the Thread to process the Execute command request.
            Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
            //Make the thread as background thread.
            objThread.IsBackground = true;
            //Set the Priority of the thread.
            objThread.Priority = ThreadPriority.AboveNormal;
            //Start the thread.
            objThread.Start(command);
        }
        catch (ThreadStartException objException)
        {
            // Log the exception
        }
        catch (ThreadAbortException objException)
        {
            // Log the exception
        }
        catch (Exception objException)
        {
            // Log the exception
        }
    }

System.Diagnostics.Process类具有一个名为WaitForExit()的方法,该方法将等待其启动的进程退出后再继续,然后将返回其返回代码。

尝试使用命令创建一个批处理文件,然后通过Process类运行该批处理文件。 如果使用Process.WaitForExit()会发生什么? 调用Process.Start(processInfo)之后; 根本没有来自process.WaitForExit()的返回码吗?

也许错误将进入StandardError流,所以您再也看不到它们了?

另外,为什么不直接通过cmd.exe而不是直接调用PDFCreator.exe?

试试这个

ProcessStartInfo processStartInfo = new ProcessStartInfo(@"c:\program files (x86)\pdfcreator");
processStartInfo.Arguments = @"/PF""c:\dwf\dwf.dwf"""
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);

// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
string errors = p.StandardError.ReadToEnd();

在serverfault发现问题所在。 我正在调用的应用程序需要在桌面上打开另一个应用程序,然后打印为PDF。 除非在services --> service name --> log选项卡中进行设置,否则IIS无法在桌面上打开程序。

不幸的是,我正在调用的应用程序不在“服务”面板中,因此我目前再次陷入困境,但至少我知道这不是C#问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM