ASP.NET:执行外部可执行文件不起作用

时间:2009-09-09 10:04:19

标签: asp.net shell process executable

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

用于在命令行上转换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-基本\ 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

2 个答案:

答案 0 :(得分:2)

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

            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

答案 1 :(得分:0)

为什么你注释掉了WaitForExit()?

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

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