【问题标题】:ASP.NET: Execute an external executable doesn't workASP.NET:执行外部可执行文件不起作用
【发布时间】:2009-09-09 10:04:19
【问题描述】:

我需要以下代码方面的帮助。 我尝试将 AutoCAD 文件从 dwg 格式转换为 dwf 格式。 然后,下载 dwf 文件并使用 java 小程序在客户端计算机上打开。

用于在命令行转换dwg文件的命令是: C:\inetpub\wwwroot\COR-Basic\cadviewer\converter\ax2008.exe -i="C:\inetpub\wwwroot\test\container\DU38_EG00_070116.dwg" -o="C:\inetpub\wwwroot\COR- Basic\cadviewer\files\DU38_EG00_070116.dwf" -f=dwf -model -text

这在我在 cmd.exe 中输入命令文本时有效。

但是当我从我的 asp.net 应用程序中调用它时,它只会启动进程,但进程永远不会结束......

我已尝试添加其他用户,已授予该用户完全权限,以及对 wwwroot 的完全权限,但仍然无法正常工作。

任何人都知道我做错了什么,或者我怎么能以另一种方式做到这一点?

  If System.IO.File.Exists(strDWGlocation) Then
        Dim psiProcessSettings As Diagnostics.ProcessStartInfo = New Diagnostics.ProcessStartInfo
        psiProcessSettings.FileName = strApplicationPath
        psiProcessSettings.Arguments = " -i=""" & strDWGlocation & """ -o=""" & strOutputLocation & """ -f=dwf -model -text"
        'ST-LAPTOP\converter
        psiProcessSettings.UserName = "converter"
        psiProcessSettings.Password = secureString

        'StefanSteiger.Debug.MsgBox("Input location:" + strDWGlocation)
        'StefanSteiger.Debug.MsgBox("Output location:" + strOutputLocation)
        Response.Write("<h1>Argument1: " + psiProcessSettings.Arguments + "</h1>")
        Response.Write("<h1>Pfad1: " + psiProcessSettings.FileName + "</h1>")


        'psiProcessSettings.RedirectStandardInput = True
        psiProcessSettings.RedirectStandardError = True
        psiProcessSettings.RedirectStandardOutput = True 'Redirect output so we can read it.
        psiProcessSettings.UseShellExecute = False 'To redirect, we must not use shell execute.
        'psiProcessSettings.CreateNoWindow = True ' don't create a window
        Dim pConverterProcess As Diagnostics.Process = New Diagnostics.Process
        pConverterProcess = Diagnostics.Process.Start(psiProcessSettings) 'Create the process.
        pConverterProcess.Start() 'Execute the process.
        'Response.Write("<h1>" + Replace(pConverterProcess.StandardOutput.ReadToEnd(), vbCrLf, "<BR />") + "</h1>") 'Send whatever was returned through the output to the client. 

        'pConverterProcess.CancelOutputRead()
        'pConverterProcess.CancelErrorRead()
        'pConverterProcess.StandardInput.Close()
        'Wait for the process to end.
        'pConverterProcess.WaitForExit()
        pConverterProcess.Close()
        'Dim iExitCode As Integer = pConverterProcess.ExitCode()
        pConverterProcess.Dispose()
    Else
        MyNamespace.Debug.MsgBox("No such file.")
    End If

【问题讨论】:

  • 你应该向 ax2008 的创建者询问这个问题。特别是,应用程序在启动时是否显示任何 UI?
  • 不,它不是,它是一个命令行实用程序。而且它不要求输入,你只需要输入命令行参数然后回车。

标签: asp.net shell process executable


【解决方案1】:

这是我做类似事情的代码,它有效!

            process.StartInfo.FileName = toolFilePath;
            process.StartInfo.Arguments = parameters;

            process.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
            process.StartInfo.WorkingDirectory = Path.GetDirectoryName(toolFilePath);

            process.StartInfo.Domain = domain;
            process.StartInfo.UserName = userName;
            process.StartInfo.Password = decryptedPassword;

            process.Start();

            output = process.StandardOutput.ReadToEnd(); // read the output here...

            process.WaitForExit(); // ...then wait for exit, as after exit, it can't read the output

            returnCode = process.ExitCode;

            process.Close(); // once we have read the exit code, can close the process

【讨论】:

  • 但不在集成管道模式下。好吧,刚刚发现,我必须将 IIS 7 切换到经典管道模式,它才开始工作。有趣的功能...
【解决方案2】:

为什么要注释掉 WaitForExit()?

您也可以尝试将 EnableRaisingEvents 设置为 true。

根据我的经验,Process 类在读取标准输出时很难使用,请尝试删除任何尝试重定向和读取输出的代码

【讨论】:

  • 我已将其注释掉,因为我认为它可能是错误源。但事实并非如此。我已经尝试省略重定向,但无济于事......我将尝试使用 enableraisingevent。
猜你喜欢
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
相关资源
最近更新 更多