簡體   English   中英

從asp.net掛起執行批處理

[英]Execute batch from asp.net hangs

我正在從ASP.NET c#頁運行批處理。 我正在嘗試使用下面鏈接中描述的System.Diagnostics.ProcessStartInfo方法。 當我單擊我的按鈕運行批處理時,頁面僅掛上“正在等待”。

我正在使用的原始代碼

以下是解決方案的一些類似問題:

可能的解決方案1

可能的解決方案2

不幸的是,我一點都不了解解決方案1,而解決方案2似乎表明我嘗試使用的路徑存在問題,但是我對此也不太清楚。 任何幫助是極大的贊賞。

我的代碼:

protected void RunPkg_Click(object sender, EventArgs e)
{
    // Get the full file path
    string strFilePath = "C:\\inetpub\\wwwroot\\DecisionSupport\\CMSBenPerfUpload\\RunPackage.bat";

    // Create the ProcessInfo object
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
    psi.UseShellExecute = false; 
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardError = true;
    psi.WorkingDirectory = "C:\\inetpub\\wwwroot\\DecisionSupport\\CMSBenPerfUpload\\";

    // Start the process
    System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

    // Open the batch file for reading
    System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

    // Attach the output for reading
    System.IO.StreamReader sOut = proc.StandardOutput;

    // Attach the in for writing
    System.IO.StreamWriter sIn = proc.StandardInput;

    // Write each line of the batch file to standard input
    while(strm.Peek() != -1)
    {
      sIn.WriteLine(strm.ReadLine());
    }

    strm.Close();

    // Exit CMD.EXE
    string stEchoFmt = "# {0} run successfully. Exiting";

    sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
    sIn.WriteLine("EXIT");

    // Close the process
    proc.Close();

    // Read the sOut to a string.
    string results = sOut.ReadToEnd().Trim();

    // Close the io Streams;
    sIn.Close(); 
    sOut.Close();

    // Write out the results.
    string fmtStdOut = "<font face=courier size=0>{0}</font>";
    this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

更新:我將路徑更改為C:\\ Temp \\,只是看它是否有所作為,但沒有。 我在正在使用的目錄上打開了安全保護蓋,不能進去。

更新2:在我的開發箱上運行此命令是相同的,但實際上我在下面的塊中得到了反饋。 如果我在cmd中手動執行此操作,則不會執行任何問題。 如果我手動運行該批處理本身,就沒有問題。

> Microsoft Windows [Version 6.1.7601]
>Copyright (c) 2009 Microsoft Corporation. All rights reserved.
>
>C:\inetpub\wwwroot\DecisionSupport\CMSBenPerfUpload>dtexec /f "ACO-SHS-PatDB.dtsx"
>Microsoft (R) SQL Server Execute Package Utility
>Version 10.50.1600.1 for 32-bit
>Copyright (C) Microsoft Corporation 2010. All rights reserved.
>
>Option "#" is not valid.
>
>C:\inetpub\wwwroot\DecisionSupport\CMSBenPerfUpload>

您的進程標識無權訪問該目錄。 您打算在哪里發布此內容的另一個問題。 明智的管理員會阻止任何網站在服務器上運行可執行文件或批處理文件

我將在這里回答一個問題,因為我討厭事情沒有解決。 我永遠無法使它以這種形式正確地工作。 我有一個網絡管理員檢查權限,只是為了確保我沒有忽略任何東西,而且他們也找不到任何有作用的東西。 所有權限似乎都被正確授予。

在我的開發PC和所有部件的服務器(Windows,SQL Server,Visual Studio)之間的版本差異之間的某個地方,問題始終被很好地隱藏。 微軟在制造產品時不能在一台機器上正常工作而又不浪費太多時間尋找解決方案而感到由衷的感謝。

無論哪種方式,我最終都走了一條不同的路線。 我在SQL 2012服務器上構建了非常簡單的SSIS程序包,可以從服務器上構建的存儲過程中執行該程序包。 這使權限和版本問題陷入困境。 然后,我使用C#腳本將XML結果文件寫到所需的目錄中。 要克服的一個問題是C#executescalar不會處理大於2033個字符的XML,因此我不得不使用ExecuteXmlReader並遍歷結果以構建字符串,然后再將其寫入文件。

盡管此問題的結局與開始時有所不同,但此問題已得到解決。

暫無
暫無

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

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