编程语言
首页 > 编程语言> > 尝试在C#/ ASP.NET / IIS 7.5 / Win7环境中调用命令提示符

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

作者:互联网

在服务器上,我试图打开命令提示符并调用可执行文件将文件转换为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
        }
    }

标签:c,shell,asp-net,iis-7-5
来源: https://codeday.me/bug/20190704/1377638.html