I want to execute a command to lock the drive through bit locker when button is clicked. How to do this? I'm new in c#
The command is:
manage-bde -lock x:
How it will be send to console? here is the code
private void btnlock_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C manage-bde -lock "+textBox1.Text+":";
process.StartInfo = startInfo;
process.Start();
}
You can use the Process class in System.Diagnostics namespace.
It should be something like this:
System.Diagnostics.Process.Start("manage-bde", "-lock x:");
The command isn't being executed because your command line doesn't know where to find the manage-bde program.
All you need to do is add the full path of the file like so:
startInfo.Arguments = #"/C C:\Program Files\Foo\manage-bde.exe -lock "+textBox1.Text+":";
Note: I'm not sure if the .exe part is necessary, but it doesn't hurt adding it in. Also, make sure you either use 2 backslashes (\\) or use the # before the quotation mark at the start.
Related
I'm using the below code to log output of a cmd call to a file however it's not working at times.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
//startInfo.Arguments = "/C dir C:\\ >c:\\temp\\dir.txt";
startInfo.Arguments = "/C \"C:\\Program Files\\Geth\\geth.exe\" --exec \"web3.eth.getBalance(web3.eth.accounts[0]);\" attach >c:\\temp\\out.txt";
process.StartInfo = startInfo;
process.Start();
The simple dir works fine.
Using the Ethereum geth.exe without --exec works fine.
However once I include the --exec argument the output is blank. Both commands work fine and produce output if manually called in cmd.exe.
"C:\Program Files\Geth\geth.exe" attach >c:\temp\out.txt
"C:\Program Files\Geth\geth.exe" --exec "web3.eth.getBalance(web3.eth.accounts[0]);" attach >c:\temp\out.txt
I see you found a workaround, but for others:
process.Start();
process.WaitForExit();
You have to wait for the process to exit.
I'm trying to run JMeter through C# with cmd but it just opens cmd and doesn't run anything.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = "D:";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/k D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar -n -t D:\\Delo\\dokument.jmx";
process.StartInfo = startInfo;
process.Start();
That code just opens the cmd and nothing happens. I've tried changing the working directory but it doesn't work. If I don't set the working directory, cmd just open at my debug directory. This does work if I start it directly from cmd (without C#).
Solved with this: a link
I'm not exactly sure what are you trying to achieve, and why you aren't using System.Diagnostics ? But I have a suggestion if I understood you right:
> System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
> startInfo.WorkingDirectory = "D:";
> startInfo.FileName = #"D:\jmeter\apache-jmeter-2.13\bin\ApacheJMeter.jar";
> startInfo.Arguments = "";
> System.Diagnostics.Process.Start(startInfo);
>
> System.Diagnostics.ProcessStartInfo startInfo2 = new System.Diagnostics.ProcessStartInfo();
> startInfo2.WorkingDirectory = "D:";
> startInfo2.FileName = #"D:\Delo\dokument.jmx";
> startInfo2.Arguments = "";
> System.Diagnostics.Process.Start(startInfo2);
I don't think you will be able to run .jar file directly via cmd interpreter, go for the following alternatives:
use jmeter.bat wrapper script
call Java executable like: path\to\java.exe -jar D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar ...
I would also suggest using -l command line argument so .jtl results file could be generated.
See How Do I Run JMeter in Non-GUI Mode? article for details. I also believe that Full list of command-line options will be helpful in your case.
So I'm using visual studio to run a command line to get a "dir /s >
c:\log.txt".
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C dir " + drivePath + " /s > c:\\log.txt";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I'm wanting to put this in a backgroundWorker so that I'm able to cancel it if the process is taking too long. But I don't see any way to cancel the execution of command line since the execution of the command is outside the scope of the program. Is there some logic I can add to this so that it will at least act as if I've cancelled it?
I need to write a windows form app, WPF, or console application that should run this from a command prompt:
#echo off
echo test
or
del c:\b.txt
The c# code needs to be on a button but It's not important now.
I tried this:
using System;
using System.Diagnostics;
using System.ComponentModel;
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
//process.StartInfo.WorkingDirectory = "c:\temp";
//process.StartInfo.Arguments = "somefile.txt";
Process.Start("cmd", "/dir");
}
but i can't put my CMD code in this ^ code.
I believe what you want is this:
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
// add /C for command, a space, then command
psi.Arguments = "/C dir";
p.StartInfo = psi;
p.Start();
The above code allows you to execute the commands directly in the cmd.exe instance. For example, to launch command prompt and see a directory, you assign the Arguments property of the ProcessStartInfo instance to "/C dir", which means execute the command called dir.
Another approach you could use is seen here:
// put your DOS commands in a batch file
ProcessStartInfo startInfo = new ProcessStartInfo("action.bat");
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = "C:\\dir";
Process.Start(startInfo);
Put the above code in your click event.
Create a batch file with your commands using instructions found at http://www.ehow.com/how_6534808_create-batch-files.html
In my code example, if you name the batch file 'action.bat' and place it in th c:dir folder, everything will work the way you want.
Cheers :)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)
I know how to execute something like this:
SomeEXE inputfile.txt
in the command prompt via C#.
The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.
Is it generally possible to obtain these outputs? Thanks.
Here is my current code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
String line = outputReader.ReadToEnd();
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;
Process process = new Process {
StartInfo = processStartInfo
};
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
Now you can read the outputStream as necessary.
I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput
Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).