eomeroff
eomeroff

Reputation: 9915

use mongodb_name does not work in C# Process

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.

Upvotes: 0

Views: 60

Answers (1)

BanksySan
BanksySan

Reputation: 28540

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
};

Upvotes: 1

Related Questions