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

发布于 2024-12-29 04:54:34 字数 980 浏览 0 评论 0原文

在服务器上,我尝试打开命令提示符并调用将文件转换为 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”文件夹。看来我已经接近这里了,有什么想法吗?

On the server, I'm attempting to open the command prompt and call an executable which converts a file to PDF. For this I am using the PDFCreator open source program.

In C# I am calling with this code:

    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""");

It runs with no error, yet yields no result. What this PDFCreator.exe does is call another program, Autodesk Design Review which opens, uses the PDF driver to print to PDF, and saves the file. The command you see works fine being running standalone by me.

From scouring other threads it seems security could be my issue. So I have gone to the PDFCreator and Design Review folders/executables and granted full access to NETWORK, NETWORK SERVICE, IIS_WPG, IIS_IUSRS, and ASP.NET Machine account (realize this is probably a security thread but will disable once i figure out source of the issue). This has not helped.

It should be noted than i can change directory using the first command above, and then create a "test123" folder in both PDFCreator and Design Review folders. Seems I am getting close here, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铃予 2025-01-05 04:54:34

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
        }
    }

SteveCalPoly and Val Akkapeddi comments are very interesting.
Anyway, I use the following methods to run executable with command prompt

    /// <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
        }
    }
要走干脆点 2025-01-05 04:54:34

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

尝试使用命令创建批处理文件,然后通过 Process 类运行批处理文件。如果使用 Process.WaitForExit(); 会发生什么?调用 Process.Start(processInfo); 后? process.WaitForExit() 是否有返回码?

The System.Diagnostics.Process class has a method called WaitForExit() which will wait for its launched process to exit before continuing and then will return its return code.

Try creating a batch file with your commands and then running the batch file via the Process class. What happens if you use Process.WaitForExit(); after you call Process.Start(processInfo); ? Is there a return code from process.WaitForExit() at all?

乜一 2025-01-05 04:54:34

也许错误会发送到 StandardError 流,所以您永远不会看到它们?

另外,为什么不直接调用 PDFCreator.exe 而不是通过 cmd.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();

Perhaps the errors are going to the StandardError stream and so you never see them?

Also, why not call PDFCreator.exe directly instead of via cmd.exe?

Try something like this

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();
全部不再 2025-01-05 04:54:34

在 serverfault 发现了这个问题。我调用的应用程序需要在桌面上打开另一个应用程序,然后打印为 PDF。除非在 services --> 中设置,否则 IIS 无法打开桌面上的程序服务名称-->登录选项卡。

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

Found out the issue over at serverfault. The app I was calling needs to open another app on the desktop and then print to PDF. IIS cannot open programs on the desktop unless set in the services --> service name --> log on tab.

Unfortunately, the app I am calling isn't in the services panel so I'm currently stuck again, but at least I know it's not a C# problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文