简体   繁体   English

WCF:如何在远程机器上启动简单的 cmd 命令

[英]WCF: how to start simple cmd command on remote machine

So i have WCF server running on remote machine.所以我在远程机器上运行WCF服务器。 This server received simple string as argument and i want this argument to execute via command line .该服务器接收简单字符串作为参数,我希望通过command line执行此参数。

This is the function on the server side that need to execute the command :这是服务器端需要执行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.正如您在此function开头所看到的,我将收到的command打印到我的console

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.所以我尝试通过传递参数notepad来启动notepad notepad ,我可以通过控制台看到命令是在服务器端收到的,所以我可以确定通信正常,但什么也没发生。

What i am doing wrong ?我做错了什么? same command on my local machine start notepad .我本地机器上的相同命令启动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 er r or when send the command to the server (but the server received 'appium' command):好的notepad工作正常(最后也没有.exe )但是当我发送命令appium (我想启动我的appium服务器)时,我得到了这个r或者当将command发送到server (但server收到了“appium” ' 命令):

System.ServiceModel.FaultException: 'The server was unable to process the request due to an internal error. System.ServiceModel.FaultException: '由于内部错误,服务器无法处理请求。 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.'有关错误的详细信息,请在服务器上打开 IncludeExceptionDetailInFaults(从 ServiceBehaviorAttribute 或从配置行为)以将异常信息发送回客户端,或者根据 Microsoft .NET Framework SDK 文档和打开跟踪检查服务器跟踪日志。

The FileName of the process is not correct, "cmd.exe".进程的文件名不正确,“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 .有关ProcessStartInfo类的详细概述,请查看此处

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.所以我认为更好的方法是将arguments设为字符串数组,并遵循以下约定:数组的第一个元素是您要启动的程序,数组的其余元素是程序的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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