当前位置:编程学习 > C#/ASP.NET >>

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

代码如下

 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(); --------------------编程问答-------------------- 运行程序后  

根本不能复制文件  
--------------------编程问答-------------------- string command = @"copy e:\1.txt f:"; --------------------编程问答-------------------- 另外哪里调用的,console或者winform应该没有问题。 --------------------编程问答-------------------- winform使用的
string command = @"copy e:\1.txt f:";也一样
我前面也调试过。
但是如果直接在CMD里写入copy e:\1.txt f: 可以复制文件
如果在应用程序里调用就不能复制 --------------------编程问答--------------------
引用 2 楼 findcaiyzh 的回复:
string command = @"copy e:\1.txt f:";


这个我前面测试过  也是一样  
--------------------编程问答-------------------- 直接用file类做就好了吗 File.Copy(path1, path2, true);
--------------------编程问答--------------------
引用 6 楼 wy811007 的回复:
直接用file类做就好了吗 File.Copy(path1, path2, true);


我其实最终想做的功能是用CMD的指令 COPY xx.TXT COM1
想把文件通过COM口拷贝到其他设备上 --------------------编程问答-------------------- 有没有高手会啊
为什么我代码调用CMD的COPY 指令没反应啊
根本不能复制文件 --------------------编程问答-------------------- 我这里试过了,在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();
            
        }

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,