簡體   English   中英

運行EXE文件的ASP.NET

[英]ASP.NET running an EXE File

我正在嘗試從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結果變量為空。 我將Windows 2008與IIS 7.0一起使用時不會引發異常

提前致謝,

埃澤奎爾

不要這樣 這只是普通的臟話,不應在ASP.NET中完成

  1. 編寫Windows服務
  2. 將請求存儲在隊列中
  3. 該服務應輪詢隊列和進程。 如果需要,請運行exe。 建議該服務位於其他服務器上。

不要這樣 這非常糟糕,無法擴展,對Web服務器不利

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

如果您使用

proc.StartInfo.RedirectStandardOutput = true;

那么您必須在流程執行時讀取流,而不是在調用之前

proc.WaitForExit();

標准錯誤流也是如此。 有關更多詳細信息,請參見MSDN文檔。

您需要在最后重新排列輸出讀數。

它希望您在waitforexit()調用之前先閱讀,因此您應該具有:


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

如果您要運行的應用程序確實是您所說的.NET應用程序,則可能根本不需要在單獨的進程中運行它。 相反,您可以利用.NET可執行文件也是程序集這一事實。 我認為Visual Studio不會讓您引用以.exe結尾的程序集,但命令行編譯器會。

我會嘗試使用命令行編譯器創建一個包裝程序集,該程序集簡單地引用可執行程序集,然后直接調用其Main()方法,並傳入通常會指定的任何命令行參數的字符串數組。 退出代碼(如果有)將是Main方法的整數返回值。 然后,您可以簡單地從ASP.NET應用程序調用包裝程序集。

根據可執行文件的功能以及與控制台的交互程度,此方法可能根本不起作用。 但是,如果它確實適合您的情況,則它的性能應該比分解一個單獨的過程好得多。

我要做的是讓ms sql作業調用可執行文件。 該可執行文件將作為SQL Server代理服務帳戶運行。

  1. 創建一個新的SQL Server作業
  2. 在作業屬性的常規頁面上為其命名
  3. 在“步驟”頁面中,創建“操作系統(CmdExec)”類型的新步驟
  4. Speeify命令,然后單擊“確定”以保存作業參數

可以使用EXEC msdb.dbo.sp_start_job @jobname調用新作業,其中@jobname是攜帶要啟動的作業名稱的變量。

請注意,啟動此作業時,exe的UI將被隱藏,並且不會顯示; 但您可以在任務管理器中找到它。

我已經在一些應用程序中采用了這種方法,特別是耗時的操作,這些操作無法在網頁上完成。

您可能需要將proc.StartInfo.LoadUserProfile屬性設置為true以便將管理員的用戶配置文件內容加載到注冊表中(默認情況下不會發生AFAIK)。

另外,運行“ hello world”程序以查看問題是否在於實際創建流程,或者流程本身在給出的上下文中運行是否存在問題,這可能是有教益的。

最后,作為嘗試縮小問題所在的步驟,您可能希望使用管理員或系統憑據運行ASP.NET進程本身,以查看ASP.NET實例的帳戶權限中是否有某些內容在運行是問題的一部分(但是請僅在進行故障排除時這樣做)。

使用以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM