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

前端 未结 2 1322
陌清茗
陌清茗 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);
    
    }
    

提交回复
热议问题