简体   繁体   中英

How can I use Process.Start() to execute program on the server-side via web service?

I created a wcf service and host in IIS. Inside it has a method to execute program on the server-side by invoking from the client. It doesn't seem to work. Nothing happens when the method is called.

System.Diagnostics.Process.Start(@"C:\MtbKill.bat");

Above is the code that doesn't work. I also tried to some process.

Process[] process = Process.GetProcessesByName("Mtb");
foreach (var item in process)
{
    try
    {
        item.Kill();
    }
    catch
    {
    }
}

It is not working too. I'm very confused that is it concerned with authorization? Because I have tried such to create folder, it doesn't have any problem.

Solution

I have searched many sources of related problems. Here is what I found Access is denied at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)

要运行批处理文件,必须运行带参数“/ c”的“cmd.exe”和文件名。

System.Diagnostics.Process.Start(@"cmd.exe /c C:\MtbKill.bat");

Solution

Sorry for editing instead of posting the answer

Here is a related problem link that solves my problem.

Access is denied at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)

Thank you for all help.

This is how i start a process:

try{
 Process.Start("osk.exe", "/C");
} catch(exception ex)
{
WriteLog("Error: Onscreen keyboard could not start.", ex);
}

this is how i kill a process:

    try
    {
      Process[] processlist = Process.GetProcesses();
      foreach (Process theprocess in processlist)
      {
        if (theprocess.ProcessName == "osk")
        {
          theprocess.Kill();
        }
      }
    }
    catch (Exception ex)
    {
      WriteLog("Error: KillKeyBoardProcess", ex);
    }

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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