简体   繁体   中英

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).

But if I try to run it from my app, I get: '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.

You can solve this issue by providing proper path for the file manage-bde .

Solution 1: When you run any console Commands from C# they will run from the following path by default:

if you run the project in Release Mode --> <Solution FolderName>\\<Project FolderName>\\bin\\Release

if you run the project in Debug Mode --> <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.

Solution 2 : You can set the path for manage-bde in Environment Variables

Solution 3: You can give the Full Path of manage-bde in your code.

Sample Code : here i'm providing complete path of exe/bat file which i would like to execute:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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