[英]C# Run command prompt from resources
I have a file that i need to run by creating a command prompt. 我有一个需要通过创建命令提示符来运行的文件。 Basically this is how it goes:
基本上是这样的:
The file's name is UArtCMD.exe, i use it by opening a command prompt in it's directory and run commands that are programmed into it, for example "uartcmd a". 该文件的名称为UArtCMD.exe,我通过在目录中打开命令提示符并运行已编程到其中的命令来使用它,例如“ uartcmd a”。 What i'm trying to do is add a gui to this software externally, and when, let's say, a button is pressed on the gui, it will run a command using the command prompt.
我想做的是从外部向该软件添加GUI,例如,当在GUI上按下按钮时,它将使用命令提示符运行命令。
I've managed to make it run off my computer without any hitches, but the problems occur occur when i install the software on a different computer. 我设法使它在我的计算机上运行没有任何障碍,但是当我在另一台计算机上安装该软件时,就会出现问题。 Because the UArtCMD is an external software, it wont be on every computer, so what i tried doing is adding it to the resources of the project and run it from there, the problem is i have no idea how to do so.
由于UArtCMD是外部软件,因此不会在每台计算机上使用,因此我尝试做的是将其添加到项目的资源中并从那里运行它,问题是我不知道该怎么做。
This is my code for running the command prompt on the computer which has the UArtCMD 这是我的代码,用于在具有UArtCMD的计算机上运行命令提示符
public static string executeLine(string command)
{
string result;
try
{
Process process = Process.Start(new ProcessStartInfo("cmd.exe", "/c " + command)
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = "C:\\UArtCMD",
RedirectStandardError = true,
RedirectStandardOutput = true
});
process.WaitForExit();
string text = process.StandardOutput.ReadToEnd();
process.Close();
MainWindow.log(text);
result = text;
}
catch (Exception ex)
{
result = ex.StackTrace;
}
return result;
}
Thanks in advance! 提前致谢!
If you need me to add any info or expand more on the question do let me know i will gladly do so. 如果您需要我添加任何信息或进一步扩展问题,请让我知道我会很乐意这样做。
Now UArtCMD.exe should always find it's way into the bin directory (even if it's built by a build agent into a strange output directory) and the code will know to look for it in the same directory as the code is running from. 现在,UArtCMD.exe应该总是找到它进入bin目录的方式(即使它是由构建代理程序构建到一个奇怪的输出目录中的),并且代码将知道在与代码运行源相同的目录中查找它。
Edit: The comment about removing the "WorkingDirectory" property from your process object is valid, you can do that instead of setting it to "Environment.CurrentDirectory", they both do the same thing. 编辑:关于从流程对象中删除“ WorkingDirectory”属性的注释是有效的,您可以这样做,而不是将其设置为“ Environment.CurrentDirectory”,它们都可以执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.