简体   繁体   English

从C#工具运行命令行脚本失败

[英]Running command line script from C# tool failing

I want to run the below code from my application: 我想从我的应用程序运行以下代码:

manage-bde -protectors -disable C:

It is working perfectly if I open up a command prompt and run from there (win8). 如果我打开命令提示符并从那里运行(win8),它的工作正常。

But if I try to run it from my app, I get: 'manage-bde' is not a recognized program. 但如果我尝试从我的应用程序运行它,我得到: 'manage-bde' is not a recognized program.

My code: 我的代码:

process1.StartInfo.RedirectStandardOutput = true;
            process1.StartInfo.UseShellExecute = false;
            process1.StartInfo.CreateNoWindow = false;
            process1.StartInfo.FileName = @"cmd.exe";
            process1.StartInfo.Arguments = @"/C manage-bde -protectors -disable C:";
            process1.Start();

What am I missing? 我错过了什么?

Reason for Failure: 失败原因:

cmd.exe could not identify your file manage-bde to proceed further. cmd.exe无法识别您的文件manage-bde以继续操作。

You can solve this issue by providing proper path for the file manage-bde . 您可以通过为文件manage-bde提供正确的路径来解决此问题。

Solution 1: When you run any console Commands from C# they will run from the following path by default: 解决方案1:当您从C#运行任何控制台命令时,默认情况下它们将从以下路径运行:

if you run the project in Release Mode --> <Solution FolderName>\\<Project FolderName>\\bin\\Release 如果您在发布模式下运行项目 - > <Solution FolderName>\\<Project FolderName>\\bin\\Release

if you run the project in Debug Mode --> <Solution FolderName>\\<Project FolderName>\\bin\\Debug 如果您在调试模式下运行项目 - > <Solution FolderName>\\<Project FolderName>\\bin\\Debug

So if you want to run any thirdparty exe files from your c# code you make sure to copy them(exe files) into respective folders. 因此,如果要从c#代码运行任何第三方exe文件,请确保将它们(exe文件)复制到相应的文件夹中。

Solution 2 : You can set the path for manage-bde in Environment Variables 解决方案2:您可以在环境变量中设置manage-bde的路径

Solution 3: You can give the Full Path of manage-bde in your code. 解决方案3:您可以在代码中提供manage-bde的完整路径。

Sample Code : here i'm providing complete path of exe/bat file which i would like to execute: 示例代码:这里我提供了我想要执行的exe / bat文件的完整路径:

Process process1 = new Process();
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.CreateNoWindow = false;
process1.StartInfo.FileName = @"cmd.exe";
process1.StartInfo.Arguments = @"/C C:\apache-jmeter-2.9\apache-jmeter-2.9\bin\jmeter.bat";
process1.Start();

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

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