简体   繁体   English

从asp.net挂起执行批处理

[英]Execute batch from asp.net hangs

I'm running a batch from my ASP.NET c# page. 我正在从ASP.NET c#页运行批处理。 I'm trying to use the System.Diagnostics.ProcessStartInfo method described in the link below. 我正在尝试使用下面链接中描述的System.Diagnostics.ProcessStartInfo方法。 When I click my button to run the batch, the page just hangs with "Waiting for ". 当我单击我的按钮运行批处理时,页面仅挂上“正在等待”。

The original code I'm using 我正在使用的原始代码

Here are some similar problems with solutions: 以下是解决方案的一些类似问题:

Possible solution 1 可能的解决方案1

Possible solution 2 可能的解决方案2

Unfortunately, I don't understand solution 1 at all and solution 2 does seem to indicate there is a problem with the paths I'm trying to use, but I'm a little unclear on this as well. 不幸的是,我一点都不了解解决方案1,而解决方案2似乎表明我尝试使用的路径存在问题,但是我对此也不太清楚。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

My code: 我的代码:

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

UPDATE: I changed the paths to C:\\Temp\\ just to see if it made a difference, but it didn't. 更新:我将路径更改为C:\\ Temp \\,只是看它是否有所作为,但没有。 I opened my security wide open on the dirs I'm using, no go. 我在正在使用的目录上打开了安全保护盖,不能进去。

UPDATE 2: Running this on my dev box is the same, but I actually get feedback shown in the block below. 更新2:在我的开发箱上运行此命令是相同的,但实际上我在下面的块中得到了反馈。 If I manually perform this in cmd, it executes no problem. 如果我在cmd中手动执行此操作,则不会执行任何问题。 If I manually run the batch itself, there's no problem. 如果我手动运行该批处理本身,就没有问题。

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

Your process identity doesn't have right to the directory. 您的进程标识无权访问该目录。 Another queston where do you plan to publish this. 您打算在哪里发布此内容的另一个问题。 A sane administrator would prvent any website to run executables or batch file on the server 明智的管理员会阻止任何网站在服务器上运行可执行文件或批处理文件

I'm going to put an answer in here because I hate leaving things unresolved. 我将在这里回答一个问题,因为我讨厌事情没有解决。 I never could get this to work exactly right in this form. 我永远无法使它以这种形式正确地工作。 I had a network admin check permissions just to make sure I wasn't overlooking something, and they couldn't find anything that made a difference either. 我有一个网络管理员检查权限,只是为了确保我没有忽略任何东西,而且他们也找不到任何有作用的东西。 All permissions seemed to be properly granted. 所有权限似乎都被正确授予。

Somewhere between the version differences in my dev PC and the server for all parts, Windows, SQL Server, Visual Studio, somewhere in there the problem kept itself well-hidden. 在我的开发PC和所有部件的服务器(Windows,SQL Server,Visual Studio)之间的版本差异之间的某个地方,问题始终被很好地隐藏。 Kudos to Microsoft for making products that fail to work properly from one machine to the next without wasting far too many hours hunting for a resolution. 微软在制造产品时不能在一台机器上正常工作而又不浪费太多时间寻找解决方案而感到由衷的感谢。

Either way, I ended up going a different route. 无论哪种方式,我最终都走了一条不同的路线。 I built very simple SSIS packages on my SQL 2012 server that could be executed from Stored Procedures built on the server. 我在SQL 2012服务器上构建了非常简单的SSIS程序包,可以从服务器上构建的存储过程中执行该程序包。 This kept permission and version issues at bay. 这使权限和版本问题陷入困境。 I then used C# scripting to write out my XML results files to the directories needed. 然后,我使用C#脚本将XML结果文件写到所需的目录中。 The one issue to overcome was that C# executescalar will not handle XML larger than 2033 characters so I had to use ExecuteXmlReader and loop through the results to built my string before writing it to file. 要克服的一个问题是C#executescalar不会处理大于2033个字符的XML,因此我不得不使用ExecuteXmlReader并遍历结果以构建字符串,然后再将其写入文件。

This problem, though ending differently than it began, is resolved. 尽管此问题的结局与开始时有所不同,但此问题已得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM