从winform C#中的命令提示符执行自定义命令 [英] Executing custom commands from command prompt in winform C#

查看:497
本文介绍了从winform C#中的命令提示符执行自定义命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方可执行命令被绑定到我的winform应用程序。该命令放置在执行应用程序的目录中名为tools的目录中。



例如,如果我的winform mytestapp.exe放置在D:\apps\mytestapp目录中,则第三方命令的路径为D:\ apps\mytestapp\tools\mycommand.exe。我使用Application.StartupPath来标识mytestapp.exe的位置,以便它可以从任何位置运行。



我通过启动一个process - System.Diagnostics.Process.Start并使用命令提示符执行。



我面临的问题是,如果我的应用程序和命令的路径没有任何空格在它,它工作正常



例如,
如果我的应用程序和命令放置如下,它工作
D:\apps\\ \\mytestapp\mytestapp.exe
D:\apps\mytestapp\tools\mycommand.exeparameter1parameter2 - this works



但是如果我在路径中有一个空格,它会失败



C:\Documents和settings\mytestapp\tools\mycommand.exeparameter1 parameter2 - does not work
C:\Documents and settings\mytestapp\tools\mycommand.exeparameter1 parameter2 - does not work
C:\Documents and settings\mytestapp \tools\mycommand.exeparameter1 parameter2 - does not work
C:\Documents and settings\mytestapp\tools\mycommand.exe parameter1 parameter2 - does not work



我试图使用双引号来执行如上所示的命令,它不工作。
那么,如何执行我的自定义命令。任何输入或解决这个问题?

 





try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(cmd,/ c+ command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
}
catch(Exception objException)
{
//记录异常
}


解决方案

尝试从命令中提取Working目录,并为ProcessStartInfo对象设置WorkingDirectory属性。然后在您的命令中只传递文件名。



此示例假设命令只包含完整的文件名。

需要根据实际命令进行调整

  string command =C:\Documents and settings\mytestapp\tools\mycommand.exe; 
string parameters =parameter1 parameter2;

try
{
string workDir = Path.GetDirectoryName(command);
string fileCmd = Path.GetFileName(command);
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(cmd,/ c+ fileCmd ++ parameters);
procStartInfo.WorkingDirectory = workDir;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
}
catch(Exception objException)
{
//记录异常
}


I have a third party executable command which is being bundled into my winform application. The command is placed inside a directory called "tools" from the directory where the application is being executed.

For example, if my winform mytestapp.exe is placed in D:\apps\mytestapp directory, then the path to the third party command is D:\apps\mytestapp\tools\mycommand.exe. I'm using Application.StartupPath to identify the location of mytestapp.exe, so that it can be run from any location.

I'm executing this command by starting a process - System.Diagnostics.Process.Start and executing the same using command prompt. There are additional parameters to be passed to run the command.

The problem I'm facing is, if the path to my application and the command does not have any white spaces in it, it works fine

For example, if my app and command is placed like below, it works D:\apps\mytestapp\mytestapp.exe D:\apps\mytestapp\tools\mycommand.exe "parameter1" "parameter2" - this works

however if I have a white space in the path, it fails

C:\Documents and settings\mytestapp\tools\mycommand.exe "parameter1" "parameter2" - doesnt work C:\Documents and settings\mytestapp\tools\mycommand.exe "parameter1 parameter2" - doesnt work "C:\Documents and settings\mytestapp\tools\mycommand.exe" "parameter1 parameter2" - doesnt work "C:\Documents and settings\mytestapp\tools\mycommand.exe parameter1 parameter2" - doesnt work

I tried using double quotes for executing the command as shown above and it doesn't work. So, how do I execute my custom command. Any inputs or work around for this issue? Thanks in advance.

Here is the code for starting the process

try
        {
            System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = true;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();
            proc.WaitForExit();
        }
        catch (Exception objException)
        {
            // Log the exception
        }

解决方案

Try to extract the Working directory from your command and set the WorkingDirectory property for the ProcessStartInfo object. Then in your command pass only the filename.

This example assumes that command contains only the full filename.
Need to be adjusted for your actual command text

string command = "C:\Documents and settings\mytestapp\tools\mycommand.exe";
string parameters = "parameter1 parameter2";

try
{
    string workDir = Path.GetDirectoryName(command);
    string fileCmd = Path.GetFileName(command);
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + fileCmd + " " + parameters);
    procStartInfo.WorkingDirectory = workDir;
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    proc.WaitForExit();
}
catch (Exception objException)
{
    // Log the exception
}

这篇关于从winform C#中的命令提示符执行自定义命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆