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
};
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();
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 start another process from my aplication:
private readonly ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = $#"{Environment.CurrentDirectory}\External\app.exe",
Arguments = "",
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
};
...
void Open()
{
this.SensorCheckProcess = new System.Diagnostics.Process();
this.SensorCheckProcess.StartInfo = this.startInfo;
this.SensorCheckProcess.Start();
this.ErrorReader = this.SensorCheckProcess.StandardError;
this.OutputReader = this.SensorCheckProcess.StandardOutput;
this.InputWriter = this.SensorCheckProcess.StandardInput;
}
I can stop process by writing exit into this.InputWriter. But I usualy stop my debugging by SHIFT+F5, and process app.exe stays open and I have to shut it manualy in Task Manager.
Is there any way, how to setup Aplication or Visual Studio to abort process automaticly.
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 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();
}