Skip to main content
 首页 » 编程设计

c#之CMD COPY 找不到文件

2024年02月20日12jyk

C# 赢得表单

嗨。 CMD窗口一直说“系统找不到指定的文件” 如果我在 CMD 窗口中输入命令“copy/b myfile.txt test.txt”,它会起作用:S

顺便说一句:有没有办法实际看到在 CMD 窗口中执行的命令?

    public void OutputBtn_Process_Click(object sender, EventArgs e) 
    { 
        foreach (FileInfo fi in listBox1.Items) 
        { 
            Process process1 = new Process(); 
            process1.StartInfo.UseShellExecute = false; 
            process1.StartInfo.Arguments = "copy /b \""+fi.Name+"\"test.txt"; 
            //process1.StartInfo.LoadUserProfile = true; 
            process1.StartInfo.FileName = "cmd.exe\"/k"; 
            process1.StartInfo.WorkingDirectory = Path.GetDirectoryName(fi.FullName); 
            process1.Start(); 
 
            process1.Dispose(); 
            process1.Close(); 
        } 
    } 

请您参考如下方法:

/K 是一个参数,应该在 Arguments 属性中而不是在 FileName 属性中 (当然,当 L.B. 建议在 test.txt 前面放置一个空格时,他是对的)

process1.StartInfo.Arguments = "/k copy /b \""+fi.Name+ "\" test.txt";  
process1.StartInfo.FileName = "cmd.exe";  

顺便说一下,您编写的代码在列表框上循环,并且在每个循环中,使用当前 FileInfo 项的内容重新编写文件 test.txt。这样,列表中的最后一个文件将是其内容被复制到 test.txt 的文件。这个逻辑对吗?