I want to run mc.exe using by PowerShell as I write below.
How can I do that? I tried to add in Filename but it doesn't work.
var mcExe = #"C:\Users\developer\Desktop\Example\mc.exe ";
var proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = mcExe;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Verb = "runas";
proc.StartInfo.Arguments = String.Format("{0}{1}{2}", "./mc alias set myCloud http://localhost:9000", "admin", "123456");
proc.Start();
Did you try set proc.StartInfo.UseShellExecute = true; ?
Because this property responsible for using powershell
Starting Powershell directly might work for you, e.g. :
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"powershell.exe",
Arguments = #"& 'C:\Users\developer\Desktop\Example\mc.exe' #('./mc alias set myCloud http://localhost:9000', 'admin', '123456')",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
};
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
Related
I'm creating Process to run pg_dump.exe in C#
var processStartInfo = new ProcessStartInfo
{
Arguments = #"-U postgres -W -f D:\postgres\test123_dump.sql postgres",
CreateNoWindow = true,
FileName = #"C:\PostgreSQL\bin\pg_dump.exe",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardInput = true
};
Process process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };
process.Start();
using( StreamWriter sw = process.StandardInput)
{
sw.WriteLine("123"); // test password
};
It will run pg_dump.exe, it will show prompt to pass the password, but StreamWriter seems to not work for some reason.
You could use this string to put your authentication info directly in argument list
var processStartInfo = new ProcessStartInfo
{
Arguments = #"--dbname=postgresql://user_name:pass_word#Localhost:5432/bd_name_to_save -F c -b -f output_bd_name",
CreateNoWindow = true,
FileName = #"C:\PostgreSQL\bin\pg_dump.exe",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardInput = true
};
Process process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };
process.Start();
This is my code and I'm using it in a while loop.
The cmd command changes every time. It means if I write "cd .." and in the next round of while loop if I write "dir" that's not gonna give me the previous folder items or in simpler language previous cmd closed and another one opens.
while (i < 99) {
System.Diagnostics.Process process = new
System.Diagnostics.Process();
process.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = #"/C " + lastMsg; //this line is a cmd command
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
string q = "";
while (!process.HasExited)
{
q += process.StandardOutput.ReadToEnd();
}
}
If you want to send all commands to the same instance of cmd, you must create this instance outside your loop, redirect the standard input, and send the commands through the standard input:
var cmdStartInfo = new ProcessStartInfo
{
FileName = "cmd",
RedirectStandardInput = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
var cmdProcess = Process.Start(cmdStartInfo);
while (i < 99)
{
cmdProcess.StandardInput.WriteLine(lastMsg);
}
Im trying to get the outpout of my CMD command and i get the wrong outpout:
here's my cmd command : cm whoami
here's the outpout i should get (CMD outpout) :
C:\Users\Joevin>cm whoami
JoevinFerret
here's my code :
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "cm whoami",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string outpout = process.StandardOutput.ReadLine();
here's the outpout that i get :
outpout = "Microsoft Windows [Version 10.0.19041.804]"
Thanks to Mathias.R and Christian.K i have been able to find a solution.
Here's my code:
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c cm whoami",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string outpout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
I am working on a GPG encryption and want to save the file to a specific directory... can some one tell me how to do this..
i am using this code
ProcessStartInfo startInfo = new ProcessStartInfo()
{
WorkingDirectory = #"C:\",
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
startInfo.FileName = "gpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-e -r myname config.xml";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
when i do this it saves it to the APPdata folder is there a way i can change it to some default folder?
do i have to set some environmental variables to do this?
Please help me.. Let me know if I am not clear or i am missing something really stupid!
Thanks in advance
ProcessStartInfo startInfo = new ProcessStartInfo()
{
WorkingDirectory = #"C:\",
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
startInfo.FileName = "gpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = #"-e -r myname c:\MYPATH\config.xml -o c:\MYPATH\config.xml.gpg";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
How can running CMD from c# without to see the cmd windows?
In ProcessStartInfo there's a parameter called CreateNoWindow
public static string ExecuteCommand(string command) {
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process proc = new Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
if (string.IsNullOrEmpty(output))
output = proc.StandardError.ReadToEnd();
return output;
}
}