Run mongoimport command via C #

1

I need to run the mongoDBD command from mongoDB via code but it is not right:

           'pro.FileName = "cmd.exe";
            //pro.UseShellExecute = false;
            pro.Arguments = @"/k cd C:\Program Files\MongoDB\Server.4\bin\";
            Process proStart = new Process();
            proStart.StartInfo = pro;
            proStart.Start();

I need to run the mongoimport inside the bin, but I can not.

Can anyone give a help?

    
asked by anonymous 05.05.2017 / 03:33

1 answer

0

You are just opening the cmd and navigating to the mongo bin folder

An example taken from SOen

    public bool importCsv(string filepath,  string collectionName)
    {

    string result ="";
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"C:\Program Files\MongoDB\Server.4\bin\mongoimport.exe";
        startInfo.Arguments = @" -d test -c " + collectionName + " --type csv --file " + filepath + " --headerline";
        Process proc = new Process();
        proc.StartInfo = startInfo;
        proc.Start();
        result += "ddd";
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }

    if (!result.Equals(""))
    {
        return true;
    }
    return false;
}

Note that the filename is itself mongoimport

 startInfo.FileName = @"C:\Program Files\MongoDB\Server.4\bin\mongoimport.exe";

Source | SOen

    
05.05.2017 / 03:44