C#中的cmd命令
cmd command in c#
我想在 C# 中使用 mongoimport 将 csv 文件导入到 mongodb。所以我实现了这个方法
public bool importCSV(string filepath, string db, string collectionName){
string result="";
try
{
ProcessStartInfo procStart = new ProcessStartInfo("cmd", "C:/MongoDB/Server/3.0/bin/mongoimport -d " + db + " -c " + collectionName + " --type csv --file " + filepath );
procStart.RedirectStandardOutput = true;
procStart.CreateNoWindow = false;
Process proc = new Process();
proc.StartInfo = procStart;
proc.Start();
result += proc.StandardOutput.ReadToEnd();
}
catch(Exception e){
Console.WriteLine(e.ToString());
}
if (!result.Equals("")){
return true;
}
return false;
}
当我运行命令自己时,我可以将文件导入MongoDB。但是通过使用 C#,方法 returns false。
谁能帮我解决这个问题?
解决方案!!!
public bool importCsv(string filepath, string collectionName){
string result ="";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:/MongoDB/Server/3.0/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;
}
尝试这样的事情:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
result+=e.Data;
}
});
process.Start();
// Asynchronously read the standard output of the spawned process.
// This raises OutputDataReceived events for each line of output.
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();
我想在 C# 中使用 mongoimport 将 csv 文件导入到 mongodb。所以我实现了这个方法
public bool importCSV(string filepath, string db, string collectionName){
string result="";
try
{
ProcessStartInfo procStart = new ProcessStartInfo("cmd", "C:/MongoDB/Server/3.0/bin/mongoimport -d " + db + " -c " + collectionName + " --type csv --file " + filepath );
procStart.RedirectStandardOutput = true;
procStart.CreateNoWindow = false;
Process proc = new Process();
proc.StartInfo = procStart;
proc.Start();
result += proc.StandardOutput.ReadToEnd();
}
catch(Exception e){
Console.WriteLine(e.ToString());
}
if (!result.Equals("")){
return true;
}
return false;
}
当我运行命令自己时,我可以将文件导入MongoDB。但是通过使用 C#,方法 returns false。
谁能帮我解决这个问题?
解决方案!!!
public bool importCsv(string filepath, string collectionName){
string result ="";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:/MongoDB/Server/3.0/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;
}
尝试这样的事情:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
result+=e.Data;
}
});
process.Start();
// Asynchronously read the standard output of the spawned process.
// This raises OutputDataReceived events for each line of output.
process.BeginOutputReadLine();
process.WaitForExit();
process.Close();