c#程序片段,替换所有同名文件

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                replacefile rf = new replacefile();
                rf.doReplace(@"F:\c1");
                rf.doReplace(@"F:\c2");
                rf.doReplace(@"F:\c3");
                Console.WriteLine("替换完成!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
    public class replacefile
    {
        public string sourceFilejs = @"F:\frmleftdown.js";//源文件
        public string sourceFilexml = @"F:\frmleftdown.xml";//源文件
        public void doReplace(string parentPath)
        {
            string[] files = Directory.GetFiles(parentPath);
            foreach (string f in files)
            {
                if (f=="frmleftdown.js") File.Copy(sourceFilejs, f, true);//替换目录下所有的同名文件
                if (f=="frmleftdown.xml") File.Copy(sourceFilexml, f, true);
            }
            string[] subdirs = Directory.GetDirectories(parentPath);
            foreach (string subdir in subdirs)
            {
                doReplace(subdir);
            }
        }
    }


 

 

 

posted @ 2012-10-27 14:17  真爱无限  阅读(560)  评论(0编辑  收藏  举报