falukky
falukky

Reputation: 1139

WCF: how to start simple cmd command on remote machine

So i have WCF server running on remote machine. This server received simple string as argument and i want this argument to execute via command line.

This is the function on the server side that need to execute the command:

public void ExecuteCmdCommand(string arguments)
    {
        log.Info(arguments);
        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = arguments;
        log.Debug(startInfo.Arguments);
        process.StartInfo = startInfo;
        process.OutputDataReceived += (sender, args) =>
        {                
            log.Info(args.Data);
        };

        process.Start();
        process.BeginOutputReadLine();
        process.WaitForExit();
    }

As you can see at the beginning of this function i am print to my console the command that received.

So i try to start notepad by pass the argument notepad, i can see via the console that the command is received on the server side so i am can be sure that the communication works but nothing happened.

What i am doing wrong ? same command on my local machine start notepad.

Update

OK notepad work fine (also without .exe at the end) but when i am send the command appium (i want to start my appium server) i got this error when send the command to the server (but the server received 'appium' command):

System.ServiceModel.FaultException: 'The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.'

Upvotes: 1

Views: 339

Answers (1)

Christos
Christos

Reputation: 53958

The FileName of the process is not correct, "cmd.exe". You could try this:

ProcessStartInfo startInfo = new ProcessStartInfo(arguments);
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;

and in the arguments pass the following:

notepad.exe

For a detailed overview of ProcessStartInfo class please have look here.

I suggest you think about the case in which you want to pass in arguments also some arguments related with the process you want to start. For instance, you may want to start notepad and open a file. The file should be passed also as an argument. So I think that a rather better approach it would be to make arguments an array of strings and follow the convention that the first element of the array is the program you want to start and the rest elements of the array are program's arguments.

Upvotes: 1

Related Questions