c# 如何调用CMD
2个回答
展开全部
/// <summary>
/// 复制文件到指定目录
/// </summary>
/// <param name="Local_file">原文件绝对路径</param>
/// <param name="Copy_file">存放绝对路径</param>
/// <returns>true</returns>
public bool Create_File(String Local_file, String Copy_file )
{
if (!System.IO.Directory.Exists(Copy_file))
{
System.IO.Directory.CreateDirectory(Copy_file);//路径不存在创建
}
if (!System.IO.File.Exists(Local_file)) // 文件不存在
{
return false;
}
else
{
StringBuilder contents = new StringBuilder();
string cmd_type = "xcopy ";
contents.Append(cmd_type);
contents.Append(Local_file);
contents.Append(" ");
contents.Append(Copy_file);
contents.Append(@"/k ");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();//启动程序
p.StandardInput.WriteLine(contents);
p.StandardInput.WriteLine("exit");
string sOutput = p.StandardOutput.ReadToEnd();
Console.WriteLine(sOutput);
return true;
}
}
/// 复制文件到指定目录
/// </summary>
/// <param name="Local_file">原文件绝对路径</param>
/// <param name="Copy_file">存放绝对路径</param>
/// <returns>true</returns>
public bool Create_File(String Local_file, String Copy_file )
{
if (!System.IO.Directory.Exists(Copy_file))
{
System.IO.Directory.CreateDirectory(Copy_file);//路径不存在创建
}
if (!System.IO.File.Exists(Local_file)) // 文件不存在
{
return false;
}
else
{
StringBuilder contents = new StringBuilder();
string cmd_type = "xcopy ";
contents.Append(cmd_type);
contents.Append(Local_file);
contents.Append(" ");
contents.Append(Copy_file);
contents.Append(@"/k ");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();//启动程序
p.StandardInput.WriteLine(contents);
p.StandardInput.WriteLine("exit");
string sOutput = p.StandardOutput.ReadToEnd();
Console.WriteLine(sOutput);
return true;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Process xcopy = new Process();
xcopy.StartInfo.FileName = "cmd.exe";
xcopy.StartInfo.CreateNoWindow = true;
xcopy.StartInfo.UseShellExecute = false;
xcopy.StartInfo.RedirectStandardError = true;
xcopy.StartInfo.RedirectStandardInput = true;
xcopy.StartInfo.RedirectStandardOutput = true;
xcopy.Start();
xcopy.WaitForExit();
xcopy.Close();
xcopy.StartInfo.FileName = "cmd.exe";
xcopy.StartInfo.CreateNoWindow = true;
xcopy.StartInfo.UseShellExecute = false;
xcopy.StartInfo.RedirectStandardError = true;
xcopy.StartInfo.RedirectStandardInput = true;
xcopy.StartInfo.RedirectStandardOutput = true;
xcopy.Start();
xcopy.WaitForExit();
xcopy.Close();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询