none
IIS下的mvc程序如何运行bat程序? RRS feed

  • 问题

  • iis进程 用户权限已经给了adminstrator,还是不能运行bat程序,本地调试可以。

             proc = new Process();
                    proc.StartInfo.FileName = Server.MapPath(@"~\generate.bat");
                    LogHelper.LogHelper.Info("proc.StartInfo.FileName"+ proc.StartInfo.FileName);
                    proc.StartInfo.Arguments = string.Format("10");//this is argument
                    proc.StartInfo.CreateNoWindow = false;
                    proc.Start();
                    LogHelper.LogHelper.Info("proc.StartInfo.Start");
                    proc.WaitForExit();
                    LogHelper.LogHelper.Info("proc.StartInfo.WaitForExit");

    日志都记录下来,也没有报错!

    如何在iis中运行bat文件呢?

    2017年8月23日 3:22

全部回复

  • 你好,

    请问你是需要在用户的设备上运行bat程序吗?由于安全反面的原因,网站是不能在用户的设备上运行bat程序的。

    你在本地调试能够成功是因为在本地调试的时候,你的电脑既是客户端,也是服务端,其实它是以服务端的身份来运行的,所以在本地测试的时候能够成功。

    我建议你在你的网站上提供一个链接,让用户可以下载bat文件到本地,然后用户在本地手动运行该bat文件。

    2017年8月23日 7:51
    版主
  • 前端脚本javascript/css是跑在浏览器中,后台代码是跑在w3wp.exe进程中。

    把proc.StartInfo.FileName 改成 cmd.exe,再把generate.bat当成参数传入。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年8月24日 0:51
  • 参考这里

    https://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp

    126 down vote accepted

    This should work. You could try to dump out the contents of the output and error streams in order to find out what's happening:

    static void ExecuteCommand(string command)
    {
        int exitCode;
        ProcessStartInfo processInfo;
        Process process;
    
        processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        // *** Redirect the output ***
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
    
        process = Process.Start(processInfo);
        process.WaitForExit();
    
        // *** Read the streams ***
        // Warning: This approach can lead to deadlocks, see Edit #2
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
    
        exitCode = process.ExitCode;
    
        Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
        Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
        Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
        process.Close();
    }
    
    static void Main()
    {
        ExecuteCommand("echo testing");
    }   

    * EDIT *

    Given the extra information in your comment below I was able to recreate the problem. There seems to be some security setting that results in this behaviour (haven't investigated that in detail).

    This does work if the bat file is not located in c:\windows\system32. Try moving it to some other location, e.g. the location of your executable. Note that keeping custom bat files or executables in the Windows directory is bad practice anyway.

    * EDIT 2 * It turns out that if the streams are read synchronously, a deadlock can occur, either by reading synchronously before WaitForExit or by reading both stderr and stdout synchronously one after the other.

    This should not happen if using the asynchronous read methods instead, as in the following example:

    static void ExecuteCommand(string command)
    {
        var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
    
        var process = Process.Start(processInfo);
    
        process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("output>>" + e.Data);
        process.BeginOutputReadLine();
    
        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("error>>" + e.Data);
        process.BeginErrorReadLine();
    
        process.WaitForExit();
    
        Console.WriteLine("ExitCode: {0}", process.ExitCode);
        process.Close();
    }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年8月24日 0:53
  • 如果还不可以,试下这个方法

    https://stackoverflow.com/questions/5884939/running-a-batch-file-from-an-asp-net-page

     

    A little google on "Faulting application cmd.exe" points me to this IIS forum.

    It seems that you cannot create a new process in the background under IIS, unless you use the CreateProcessWithLogon method. (I have not tested this).


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年8月24日 0:55
  •  服务端BAT,一般用任务计划或着写个sevice 控制台,,然后通过网站修改数据库或其它数据,

    sevice根据数据库或文件的状态调用bat

    需要云服务器,不是虚拟主机。。。

    现在的行情,云服务器价格合算。。。功能强大。。。总的来说,可能实现很多复杂功能~~


    2017年9月8日 0:25