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();
Related
I have a list of a thousand items, Each of these items must be checked by the CMD.exe, With the help of the following code, I can check an item by CMD
var p = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
FileName = "cmd",
Arguments = $"list {Id}"
}};
p.Start();
var _Data = await p.StandardOutput.ReadToEndAsync();
But the question is, I want all of these items to be checked quickly by CMD, I'm currently doing this as follows
foreeach(var item in list)
{
var p = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
FileName = "cmd",
Arguments = $"list {item}"
}};
p.Start();
var _Data = await p.StandardOutput.ReadToEndAsync();
}
But it takes a long time to do this
You can redirect standard input and use a StreamWriter to write to it:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
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();
What is wrong with the following code?
ProcessStartInfo startInfo = default(ProcessStartInfo);
startInfo = new ProcessStartInfo(SetupProgramPath)
{
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false
};
Process.Start(startInfo);
It is expected to prompt for credentials but nothing shows up. The system has the UAC enabled and not supposed to be changed.
I appreciate your help in this one. Thank you in advance.
I have worked this out with the following code
ProcessStartInfo startInfo = default(ProcessStartInfo);
startInfo = new ProcessStartInfo(SetupProgramPath)
{
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Normal,
FileName = "msiexec",
Arguments = "/i \"" + SetupProgramPath + "\"",
CreateNoWindow = false
};
Process.Start(startInfo);
If you want to ask the user to enter credentials of a different user, then use "runasuser":
ProcessStartInfo startInfo = new ProcessStartInfo(SetupProgramPath)
{
UseShellExecute = true,
Verb = "runasuser",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false
};
Process.Start(startInfo);
I regular use command
use some_mongodb
to create new database from command prompt on windows
it seems that this command does not work when you want to execute this in C# process
I have the following code trying to create mongo database from C#
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WorkingDirectory = _mongoBinDir,
FileName = "mongo.exe",
Arguments = "use " + databaseTxt.Text
};
_mongoInsertProcess = new Process
{
StartInfo = startInfo
};
_mongoInsertProcess.Start();
string stderrStr = _mongoInsertProcess.StandardError.ReadToEnd();
string stdoutStr = _mongoInsertProcess.StandardOutput.ReadToEnd();
stdoutStr variable get value
"MongoDB shell version: 3.2.1 connecting to: use
2016-04-07T15:28:52.875+0200 E - [main] file [some_db] doesn't
exist failed to load: some_db"
Please advise on this.
use some_db isn't a valid argument. Just pass the name of the database i.e:
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WorkingDirectory = _mongoBinDir,
FileName = "mongo.exe",
Arguments = databaseTxt.Text
};
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();
}