使用通配符并保持文件名的C#复制多个文件 [英] C# copying multiple files with wildcards and keeping file names

查看:133
本文介绍了使用通配符并保持文件名的C#复制多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要多个文件从目录使用,这并不包含完整的信息一个文本复制



NCR.txt:结果



目标目录中有:结果
red1.txt结果
red3.txt结果
red44.txt



DEST目录需要有:结果
red1.txt结果
red3.txt结果
red44.txt



我的代码:

  System.IO.Directory.CreateDirectory( @C:\\\
Prep\+ textBox1.Text +\\red);
如果(checkBox3.Checked)
{
的String [] = file_names File.ReadAllLines(@C:\NCR.txt);

的foreach(在file_names串FILE_NAME)
{
的String []文件= Directory.GetFiles(textBox2.Text,FILE_NAME +的* .txt);
的foreach(在文件中字符串的文件)
System.IO.File.Copy(文件,@C:\\\
Prep\+ textBox1.Text +\\red\\ );
}

}


解决方案

所有的建议都非常棒,thansk所有的提醒,但这是完美的:

 如果(checkBox3.Checked)
{
的String [] =行File.ReadAllLines(@C:\NCR.txt);

的foreach(以线串线)
{
的String []文件= Directory.GetFiles(textBox2.Text,行+的* .txt);
的foreach(在文件中字符串的文件)
{
FileInfo的FILE_INFO =新的FileInfo(文件);
File.Copy(文件,@C:\InPrep\+ textBox1.Text +\\text\\+ file_info.Name);
}
}
}


I need to copy multiple files from a directory using a textfile that doesnt contain complete info.

NCR.txt:
Red

target directory has in it:
red1.txt
red3.txt
red44.txt

dest directory needs to have:
red1.txt
red3.txt
red44.txt

My code:

System.IO.Directory.CreateDirectory(@"C:\nPrep\" + textBox1.Text + "\\red");
        if (checkBox3.Checked)
        {
            String[] file_names = File.ReadAllLines(@"C:\NCR.txt");

            foreach (string file_name in file_names)
            {
                string[] files = Directory.GetFiles(textBox2.Text, file_name + "*.txt");
                foreach (string file in files)
                    System.IO.File.Copy(file, @"C:\nPrep\" + textBox1.Text + "\\red\\");
            }

        }

解决方案

All suggestions were great and thansk for all the advise but this was perfect:

if (checkBox3.Checked)
{
    string[] lines = File.ReadAllLines(@"C:\NCR.txt");

    foreach (string line in lines)
    {
        string[] files = Directory.GetFiles(textBox2.Text, line + "*.txt");
        foreach (string file in files)
        {
             FileInfo file_info = new FileInfo(file);
             File.Copy(file, @"C:\InPrep\" + textBox1.Text + "\\text\\" + file_info.Name);
        }
    }
}

这篇关于使用通配符并保持文件名的C#复制多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆