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

纯分享1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions; 

namespace WindowsFormsApplication5
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Console.WriteLine("start....");
            ToMerge toMerge = ToMerge.getToMerge();
            toMerge.Init();
            Console.WriteLine("Done!!!");
            

        }
    }

    partial class ToMerge
    {
        private static ToMerge toMerge;
        private string file_Name = "";//Summary文件夹路径
        private string EPCMSTTP = "";//EPCMSTTP.txt文件路径
        private string fileName = Application.StartupPath;//当前路径
        private string appName = Application.ProductName;

        public static ToMerge getToMerge()
        {
            if (toMerge == null)
            {
                toMerge = new ToMerge();
            }
            return toMerge;
        }

        /// <summary>
        /// 运行方法
        /// </summary>
        public void Init()
        {
            checkSummary();
            checkEPCMSTTP();
            try
            {
                bool b = false;
                DirectoryInfo TheFolder = new DirectoryInfo(fileName);

                List<string> prefixNameList = PrefixName(TheFolder);

                if (prefixNameList.Count > 0)
                {
                    foreach (string pfName in prefixNameList)
                    {
                        string newFile_Name = file_Name + @"\" + pfName;
                        if (!Directory.Exists(newFile_Name))
                        {
                            Directory.CreateDirectory(newFile_Name);
                        }

                        foreach (FileInfo NextFile in TheFolder.GetFiles())
                        {
                            //Regex r = new Regex(name);
                            Regex r = new Regex(pfName);
                            Match m = r.Match(NextFile.Name);
                            if (m.Success && m.Index == 0)
                            {
                                NextFile.CopyTo(newFile_Name + @"\" + NextFile.Name, true);
                                b = true;
                            }

                        }

                        foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
                        {
                            //Regex r = new Regex(name);
                            Regex r = new Regex(pfName);
                            Match m = r.Match(NextFolder.Name);
                            if (m.Success && m.Index == 0)
                            {
                                toFor(NextFolder.Name, NextFolder.FullName, newFile_Name);

                                b = true;
                            }

                        }

                        if (!File.Exists(file_Name + @"\EPCMSTTP.txt"))
                        {
                            File.Copy(EPCMSTTP, file_Name + @"\EPCMSTTP.txt");
                            
                        }

                    }
                }
                if (b)
                {
                    MessageBox.Show("整合完成!");
                }
                else
                    MessageBox.Show("没有符合相同前缀的文件!");

                //file_Name = "";
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 检测“summary”是否存在,不存在就创建
        /// </summary>
        private void checkSummary()
        {
            if (!Directory.Exists(Path.GetFullPath("./Summary")))
            {
                Directory.CreateDirectory(Path.GetFullPath("./Summary"));
                
            }
            file_Name = Path.GetFullPath("./Summary");
        }

        /// <summary>
        /// 检测“EPCMSTTP.txt”是否存在,不存在就创建
        /// </summary>
        private void checkEPCMSTTP()
        {
            if (!File.Exists(fileName + @"\EPCMSTTP.txt"))
            {
                File.Create(fileName + @"\EPCMSTTP.txt");

            }
            EPCMSTTP = fileName + @"\EPCMSTTP.txt";
        }

        /// <summary>
        /// 递归复制文件
        /// </summary>
        /// <param name="name">文件名</param>
        /// <param name="fileName">完整路径</param>
        /// <param name="addr">目标完整路径</param>
        private void toFor(string name, string fileName, string addr)
        {

            string newAddr = addr + @"\" + name;
            if (!Directory.Exists(newAddr))
            {
                Directory.CreateDirectory(newAddr);
                if (!File.Exists(newAddr + @"\EPCMSTTP.txt"))
                {
                    File.Copy(EPCMSTTP, newAddr + @"\EPCMSTTP.txt");
                }
            }

            DirectoryInfo TheFolder = new DirectoryInfo(fileName);

            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                NextFile.CopyTo(newAddr + @"\" + NextFile.Name, true);

            }

            foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
            {
                toFor(NextFolder.Name, NextFolder.FullName, newAddr);

            }

            if (!File.Exists(addr + @"\EPCMSTTP.txt"))
            {
                File.Copy(EPCMSTTP ,addr + @"\EPCMSTTP.txt");
            }
        }

--------------------编程问答-------------------- 来接分的
 
此消息通过 【CSDN论坛 Winform正式版】 回复!有关此工具 --------------------编程问答-------------------- 纳尼?这是什么,代码还不全 --------------------编程问答-------------------- 分享帖一般都是推荐帖
唉,现在阿猫阿狗发点代码都是推荐帖

--------------------编程问答-------------------- 向上吧!青鸟 --------------------编程问答-------------------- mh_ma

--------------------编程问答--------------------
引用 3 楼  的回复:
分享帖一般都是推荐帖
唉,现在阿猫阿狗发点代码都是推荐帖

呃~~~~~ --------------------编程问答-------------------- 这是啥,复制所有文件 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,