C#软件中调用CMD copy d:\XX.TXT e:指令

xuelingqq 2011-07-15 06:39:29
代码如下

string command = @"copy e:1.txt f:";

//实例化一个进程类
Process p = new Process();

// p.StartInfo.CreateNoWindow = true;

//获得系统信息,使用的是 cmd.exe 这个控制台程序
p.StartInfo.FileName = "cmd.exe";

//p.StartInfo.Arguments = "/c" + command;

//将cmd的标准输入和输出全部重定向到.NET的程序里
p.StartInfo.UseShellExecute = false;//此处必须为false否则引发异常

p.StartInfo.RedirectStandardInput = true;//标准输入

p.StartInfo.RedirectStandardOutput = true;//标准输出

p.StartInfo.RedirectStandardError = true;

//不显示命令行窗口界面
//p.StartInfo.CreateNoWindow = true;
p.Start();

p.StandardInput.WriteLine(command);
//
// p.StandardInput.WriteLine("exit");

p.Close();
...全文
191 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2011-07-17
  • 打赏
  • 举报
回复
我这里试过了,在winform里,是可以的。

我的代码:

private void button1_Click(object sender, EventArgs e)
{
string command = @"copy c:\a.txt f:";

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");

p.Close();

}

xuelingqq 2011-07-16
  • 打赏
  • 举报
回复
有没有高手会啊
为什么我代码调用CMD的COPY 指令没反应啊
根本不能复制文件
xuelingqq 2011-07-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wy811007 的回复:]
直接用file类做就好了吗 File.Copy(path1, path2, true);
[/Quote]

我其实最终想做的功能是用CMD的指令 COPY xx.TXT COM1
想把文件通过COM口拷贝到其他设备上
wy811007 2011-07-16
  • 打赏
  • 举报
回复
直接用file类做就好了吗 File.Copy(path1, path2, true);
xuelingqq 2011-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 findcaiyzh 的回复:]
string command = @"copy e:\1.txt f:";
[/Quote]

这个我前面测试过 也是一样
xuelingqq 2011-07-16
  • 打赏
  • 举报
回复
winform使用的
string command = @"copy e:\1.txt f:";也一样
我前面也调试过。
但是如果直接在CMD里写入copy e:\1.txt f: 可以复制文件
如果在应用程序里调用就不能复制
宝_爸 2011-07-15
  • 打赏
  • 举报
回复
另外哪里调用的,console或者winform应该没有问题。
宝_爸 2011-07-15
  • 打赏
  • 举报
回复
string command = @"copy e:\1.txt f:";
xuelingqq 2011-07-15
  • 打赏
  • 举报
回复
运行程序后

根本不能复制文件

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧