none
服务器端执行exe RRS feed

  • 问题

  • 我在web开发时候,遇到一个棘手的问题,希望在这里得到解决。

    我的思路是想将一个计算软件集成在服务器里,用户提交数据,服务器端调用计算软件,最后返回结果到浏览器端。我写好了代码,在vs2010中运行没问题,在iis中布置不报错,就是不调用计算软件。代码如下:

            Process proc = new Process();
            string filename = Server.MapPath("App_Data\\cmd.exe");
            proc.StartInfo.FileName = filename;
          
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//隐藏窗口,后台运行
            proc.EnableRaisingEvents = true;//在进程终止时,激发 Exited 事件。
            proc.Exited += new EventHandler(proc_Exited);//在进程退出时发生,用于跳转下一个页面

            proc.Start();
            proc.StandardInput.WriteLine("cd E:");
            proc.StandardInput.WriteLine("cd E:\\iFlora_protectedplants\\BLAST");
            string inputpath = Server.MapPath("App_Data\\input.txt");
            string outputpath = Server.MapPath("App_Data\\output.xml");
            proc.StandardInput.WriteLine("blastn -query " + inputpath + " -db protected_plants -out " + outputpath + "  -outfmt 5");
        
            proc.StandardInput.WriteLine("exit");
            string stroutput = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();
            proc.Close();
            proc.Dispose();

    网上很多人都说是iis权限的问题,大部分我都试过了,都不行。而且有些人说即使实现了,这种方式也很危险。在这里提问,我希望知道,怎么才能使这段代码执行。有没有更好的方法实现服务器端执行exe。希望得到详细一些的解答。先感谢大家!

    2013年12月2日 6:59

答案

  • 你好:

    欢迎来到MSDN中文论坛。

    首先请你参考一下微软支持的一篇文章:Unable to Start a Process from ASP.NET

    里面很清楚的解释了一件事情:如果在服务端调用Process的话,进程可以在后台执行,但是你看不到执行的窗口,你甚至可以在任务管理器中看到进程名称。

    原因:

    1. By default, ASP.NET runs its worker process (Aspnet_wp.exe) with a weak account (the local machine account, which is named ASPNET) which don't have permission to interact with desktop.
    2. By default, IIS Admin Service won't allow its application to interact with desktop.

    解决办法:

    1. Give permission for ASP.NET worker process account (<localmachine>\ASPNET) to interact with desktop or allow ASP.NET worker process to run in SYSTEM account.
     
                 To know how to allow worker process to run in SYSTEM account and to know the default permissions of ASPNET account, check this article
                  INFO: Process and Request Identity in ASP.NET (http://support.microsoft.com/default.aspx?scid=kb;en-us;317012)
     
    2. Enable IIS Admin Service to interact with desktop
     
                 To configure this, follow this steps.
     
                 a. Open Control Panel and follow these steps: 
                               For Windows NT: click Services. 
                               For Windows 2000, Windows XP, and .NET Server: click Administrative Tools, and then click Services.

                 b. Double-click IIS Admin Service. 

                 c. On the Log On tab, select the Allow Service to Interact with Desktop check box.
                     Note: This article assumes that the IIS Admin Service runs as a local system.
     
                 d. Stop and restart the IIS Admin Service.      


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月3日 6:21
    版主
  • 权限问题,楼上有提到如何做。

    但是,这种非常规做法不好维护和排错,比如你现在就遇到无法正常使用的问题。

    所以,遇到这类需求,通常的解决方案是,建立一个桌面应用程序或 Windows 服务,你的Asp.Net 可以通过任意进程间通讯手段同该程序交互,从而完成你的需求。

    2013年12月3日 6:27

全部回复

  • 你好:

    欢迎来到MSDN中文论坛。

    首先请你参考一下微软支持的一篇文章:Unable to Start a Process from ASP.NET

    里面很清楚的解释了一件事情:如果在服务端调用Process的话,进程可以在后台执行,但是你看不到执行的窗口,你甚至可以在任务管理器中看到进程名称。

    原因:

    1. By default, ASP.NET runs its worker process (Aspnet_wp.exe) with a weak account (the local machine account, which is named ASPNET) which don't have permission to interact with desktop.
    2. By default, IIS Admin Service won't allow its application to interact with desktop.

    解决办法:

    1. Give permission for ASP.NET worker process account (<localmachine>\ASPNET) to interact with desktop or allow ASP.NET worker process to run in SYSTEM account.
     
                 To know how to allow worker process to run in SYSTEM account and to know the default permissions of ASPNET account, check this article
                  INFO: Process and Request Identity in ASP.NET (http://support.microsoft.com/default.aspx?scid=kb;en-us;317012)
     
    2. Enable IIS Admin Service to interact with desktop
     
                 To configure this, follow this steps.
     
                 a. Open Control Panel and follow these steps: 
                               For Windows NT: click Services. 
                               For Windows 2000, Windows XP, and .NET Server: click Administrative Tools, and then click Services.

                 b. Double-click IIS Admin Service. 

                 c. On the Log On tab, select the Allow Service to Interact with Desktop check box.
                     Note: This article assumes that the IIS Admin Service runs as a local system.
     
                 d. Stop and restart the IIS Admin Service.      


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月3日 6:21
    版主
  • 权限问题,楼上有提到如何做。

    但是,这种非常规做法不好维护和排错,比如你现在就遇到无法正常使用的问题。

    所以,遇到这类需求,通常的解决方案是,建立一个桌面应用程序或 Windows 服务,你的Asp.Net 可以通过任意进程间通讯手段同该程序交互,从而完成你的需求。

    2013年12月3日 6:27
  • 一般IIS 用户是没有权限操作EXE的,需要做伪装
    2013年12月3日 6:30