Need to execute *.exe in server from ASP.net

前端 未结 2 1318
陌清茗
陌清茗 2020-11-29 08:52

My current situation is that I need to execute an exe(which creates a local .txt file) in remote server with IIS hosting an ASP.net/C# API. I created a local user(say userA)

相关标签:
2条回答
  • 2020-11-29 09:00

    You can use Process.Start

    Process process = new Process();
    process.StartInfo.FileName = "CVS.exe";
    process.StartInfo.Arguments = "if any";
    process.Start();
    

    There is also a post about running processes as another user in asp.net:

    http://weblogs.asp.net/hernandl/archive/2005/12/02/startprocessasuser.aspx

    Supplying user credential

    In short it says that you must redirect the process, with code like this:

    ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
    
    info.UseShellExecute = false;
    
    info.RedirectStandardInput = true;
    
    info.RedirectStandardError = true;
    
    info.RedirectStandardOutput = true;
    
    info.UserName = dialog.User; // see the link mentioned at the top
    
    info.Password = dialog.Password;
    
    using (Process install = Process.Start(info))
    
    {
    
          string output = install.StandardOutput.ReadToEnd();
    
          install.WaitForExit();
    
          // Do something with you output data
    
          Console.WriteLine(output);
    
    }
    
    0 讨论(0)
  • 2020-11-29 09:01

    UPDATE: I managed to solve the issue after many weeks. Thank you all for your contribution. Apparently IIS does not load windows user profiles by default. So when running as a different user who is not logged on, their windows profile must be loaded by IIS. In advanced setting menu of your app pool, there is an option "load windows profile" I just changed this to true. In prior versions of IIS, this was set to 'true' by default.

    Related questions on SO with same solution:

    1) Security exceptions in ASP.NET and Load User Profile option in IIS 7.5

    2) Running a asp.net web application project on IIS7 throws exception

    3) System.Web.AspNetHostingPermission Exception on New Deployment

    Another 4) http://geekswithblogs.net/ProjectLawson/archive/2009/05/05/iis-system.web.aspnethostingpermission-exception-on-windows-7-rc.aspx

    0 讨论(0)
提交回复
热议问题