Skip to content

Running CommandPrompt from a Windows Service

Tracy Baird

Asked Nov 3, 2009, 6:59 AM EST3 answers
Share
LinkedInX
I have a Windows Service that will create a Process object and fire the command prompt with a specific command. The problem I am having is that apparently the command prompt is trying to open a window and I am getting the following error: Error: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application. (System.InvalidOperationException) Can anyone help me to make this fire in the background using a windows service or do I have to go another way?

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hai u need to first install the service u have written and then only make it run..


    Here is an article which shows step by step to create a windows service.

    http://aspalliance.com/1316_Working_with_Windows_Service_Using_Visual_Studio_2005.all

    Hope it helps.

    -----------------------------------------------------------------------------------------------
    If my answer helped u don't forget to mark it as correct answer
    0
  • 0
    I have installed the service and it runs up and to the point when it tries to fire the command prompt using this code: Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.Arguments = @"/C blahblah.exe"; process.Start(); string results = process.StandardOutput.ReadToEnd(); That is where i get the error message.
    0
  • 0
    Hai here is the code.Try this..-

    string commandLine = "notepad.exe";

    ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe");

    PSI.RedirectStandardInput = true;

    PSI.RedirectStandardOutput = true;

    PSI.RedirectStandardError = true;

    PSI.UseShellExecute = false;

    PSI.CreateNoWindow = false;

    Process p = Process.Start(PSI);

    System.IO.StreamWriter SW = p.StandardInput;

    System.IO.StreamReader SR = p.StandardOutput;

    SW.WriteLine(commandLine);

    SW.Close();


    Here replace the command line string with the process u want to run or aanything u want to do..Here i have opened a notepad file..

    Hope this helps....Fell free to ask..






    ----------------------------------------------------------------------------------------------
    If my answer helped u don't forget to mark it as correct answer
    0