This question already has an answer here:
Run process as administrator from a non-admin application
(1 answer)
Closed 1 year ago.
I'm trying to run this command in cmd from c# but for some reason it doesn't work. But when I change my Argument to "/C ipconfig" it work perfectly. I don't understand why. What am I doing wrong?
Here is my code:
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/C wbadmin start backup - backupTarget:{TargetBackupDrive}: -include:{LocationOs}: -allcritical - quiet";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Verb = "runas";
process.Start();
CmdOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
The verb "runas" does not work when UseShellExecute is set to false as it is pointed out here: Set ProcessStartInfo.EnvironmentVariables when Verb="runas"
You could either provide admin credentials with the UserName and Password properties our set UseShellExecute to true and execute your command in the shell.
Related
I want to start a c# process as another user.
I know the verb "runas" but that only accepts admin user creds.
//This code actually meant to restart the appplication with specific args
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = args;
startInfo.Verb = "runas"; //this line is the question
Process p = Process.Start(startInfo);
I want to achieve the same thing but also with non-admin users.
This question already has answers here:
Execute multiple command lines with the same process using .NET
(10 answers)
Closed 3 years ago.
I need to run cmd commands in a c# program im writing, the only solutions ive found make a new command line instance for every command, but i need them to be executed in the same instance
Run Command Prompt Commands
public static void UpdateDeviceData()
{
Process.Start("CMD.exe",
String.Format("client_commandline.exe setdeviceposition 0 {0} {1} {2}", LPos.X, LPos.Y, LPos.Z));
}
i found a solution, its this:
Process CmdPro;
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.FileName = "CMD.exe";
CmdPro = new Process();
CmdPro.StartInfo = startInfo; CmdPro.Start();
Console.SetOut(CmdPro.StandardInput);
I have an app running as Network Service (I can't change this) and need to run a command (execute as bat script) as a known local user. I seem to get no response and the script doesn't execute. The odd thing is that if i run the code as the local user its works without an issue.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\Windows\System32\cmd.exe";
startInfo.Arguments = "/C " + #"c:\example\script.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
var sspw = new SecureString();
foreach (var c in "MyPassword")
{
sspw.AppendChar(c);
}
startInfo.Domain = Environment.MachineName;
startInfo.UserName = "MyUser";
startInfo.Password = sspw;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I believe you need to set the working directory when using a Username/Password
according to MSDN...
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
Not sure whether this solution will help your need but if set the WorkingDirectory to current directory (".") then it should work. Basically the user who is running the process should have permission on the folder that you mention.
I want to run a command from C# with below code but the application waits infinitely:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.Start();
p.WaitForExit();
p.StandardOutput.ReadToEnd();
I did a debug and the last break point passed is p.WaitForExit(). What am I doing wrong?
By the way, I tried run the command manually and I get the result in miliseconds.
Try prepending a /C to your arguments - without it, cmd.exe won't terminate
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).