0

我正在尝试从 ASP.NET 网站运行旧的 .NET 应用程序。在阅读了 web 和 Stackoverflow(对于类似的问题)之后,我来到了以下代码。问题是我总是得到一个错误代码(我使用管理员帐户只是为了测试目的)。如果我手动运行 exe,它可以正常工作。

private void Execute(string sPath)
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.UserName = "administrador";
    string pass = ".............";

    System.Security.SecureString secret = new System.Security.SecureString();
    foreach (char c in pass) secret.AppendChar(c);

    proc.StartInfo.Password = secret;
    proc.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["WORKINGDIRECTORY"].ToString();
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.FileName = sPath;
    proc.Start();
    proc.WaitForExit();
    string result = proc.StandardOutput.ReadToEnd();
    Response.Write(result + " - "  + proc.ExitCode);
    proc.Close();
}

}

我得到的退出代码是:-1066598274 结果变量为空。没有抛出异常我正在使用带有 IIS 7.0 的 Windows 2008

提前致谢,

以西结

4

8 回答 8

4

不要这样做。这很脏,不应该从 ASP.NET 完成

  1. 写一个windows服务
  2. 将请求存储在队列中
  3. 服务应该轮询队列和进程。如果需要运行exe。建议服务留在不同的服务器上。

不要这样做。这对 Web 服务器来说非常糟糕且不可扩展且不好

于 2009-06-24T17:18:50.967 回答
2
    protected void btnSubmit_Click(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {
            litMessage.Visible = true;

            System.Diagnostics.Process oProcess = null;

            try
            {
                string strRootRelativePathName = "~/Application.exe";

                string strPathName =
                    Server.MapPath(strRootRelativePathName);

                if (System.IO.File.Exists(strPathName) == false)
                {
                    litMessage.Text = "Error: File Not Found!";
                }
                else
                {
                    oProcess =
                        new System.Diagnostics.Process();

                    oProcess.StartInfo.Arguments = "args";

                    oProcess.StartInfo.FileName = strPathName;

                    oProcess.Start();

                    oProcess.WaitForExit();

                    System.Threading.Thread.Sleep(20000);

                    litMessage.Text = "Application Executed Successfully...";
                }
            }
            catch (System.Exception ex)
            {
                litMessage.Text =
                    string.Format("Error: {0}", ex.Message);
            }
            finally
            {
                if (oProcess != null)
                {
                    oProcess.Close();
                    oProcess.Dispose();
                    oProcess = null;
                }
            }
        }
    }
于 2014-09-13T10:07:26.827 回答
1

如果你使用

proc.StartInfo.RedirectStandardOutput = true;

那么你必须在进程执行时读取流,而不是在调用之前

proc.WaitForExit();

标准错误流也是如此。有关更多详细信息,请参阅MSDN文档。

于 2009-06-24T18:00:35.513 回答
1

您需要在最后重新排序输出读数。

它希望您在 waitforexit() 调用之前阅读,因此您应该:

proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Response.Write(result + " - "  + proc.ExitCode);
proc.WaitForExit();
proc.Close();

于 2009-06-24T18:46:51.770 回答
1

如果您尝试运行的应用程序确实是如您所说的 .NET 应用程序,您可能根本不需要在单独的进程中运行它。相反,您可以利用 .NET 可执行文件也是程序集这一事实。我认为 Visual Studio 不会让您引用以 .exe 结尾的程序集,但命令行编译器会。

我会尝试使用命令行编译器创建一个包装程序集,该程序集仅引用可执行程序集,并直接调用其 Main() 方法,传入您通常指定的任何命令行参数的字符串数组。退出代码(如果有)将是 Main 方法的整数返回值。然后您可以简单地从您的 ASP.NET 应用程序中调用您的包装程序集。

根据可执行文件的功能以及它与控制台的交互程度,这种方法可能根本不起作用。但是,如果它确实适用于您的情况,那么它的性能应该比启动一个单独的进程要好得多。

于 2009-06-24T21:18:37.050 回答
1

我所做的是让可执行文件由 ms sql 作业调用。可执行文件将作为 SQL Server 代理服务帐户运行。

  1. 创建一个新的 sql server 作业
  2. 在工作属性的常规页面中为其命名
  3. 在步骤页面中,创建类型为操作系统 (CmdExec) 的新步骤
  4. 指定命令并单击确定以保存作业参数

可以使用 EXEC msdb.dbo.sp_start_job @jobname 调用新作业,其中@jobname 是带有要启动的作业名称的变量。

注意,当这个job启动时,exe的UI会被隐藏,不会显示;但您可以在任务管理器中找到它。

我在几个应用程序中使用了这种方法,尤其是在网页上无法完成的耗时操作。

于 2012-07-23T18:46:13.217 回答
0

您可能需要将该proc.StartInfo.LoadUserProfile属性设置为,true以便将管理员的用户配置文件内容加载到注册表中(AFAIK 默认情况下不会发生这种情况)。

此外,运行“hello world”程序以查看问题是否与实际创建进程有关,或者进程本身是否在给定的上下文中运行时出现问题,这可能具有教育意义。

最后,作为尝试缩小问题所在的一个步骤,您可能希望使用管理员或系统凭据运行 ASP.NET 进程本身,以查看运行 ASP.NET 实例的帐户权限中是否存在某些内容是问题的一部分(但请这样做仅用于故障排除)。

于 2009-06-24T17:09:43.227 回答
0

使用以下代码:

    ProcessStartInfo info = new ProcessStartInfo("D:\\My\\notepad.exe");
    info.UseShellExecute = false;
    info.RedirectStandardInput = true;
    info.RedirectStandardError = true;
    info.RedirectStandardOutput = true;
    //info.UserName = dialog.User;   
    info.UserName = "xyz";
    string pass = "xyz";
    System.Security.SecureString secret = new System.Security.SecureString();
    foreach (char c in pass)
        secret.AppendChar(c);
    info.Password = secret;
    using (Process install = Process.Start(info))
    {
        string output = install.StandardOutput.ReadToEnd();
        install.WaitForExit();
        // Do something with you output data          
        Console.WriteLine(output);
    }
于 2012-01-31T11:23:54.263 回答