发新话题
打印

[原创]C#整个目录拷贝与删除的类

[原创]C#整个目录拷贝与删除的类

/*
  作者:网事如风
*/
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;

namespace DYBC
{
       
        public class FileExec
        {

                public FileExec()
                {

                }
               
                private static void CopyDirectory(DirectoryInfo OldDirectory, DirectoryInfo NewDirectory)
                {
                        string NewDirectoryFullName = NewDirectory.FullName + @"\" + OldDirectory.Name;

                        if ( !Directory.Exists(NewDirectoryFullName) )
                                Directory.CreateDirectory(NewDirectoryFullName);
                       
                        FileInfo[] OldFileAry = OldDirectory.GetFiles();
                        foreach (FileInfo aFile in OldFileAry)
                                File.Copy(aFile.FullName, NewDirectoryFullName +  @"\" +  aFile.Name, true);

                        DirectoryInfo[]        OldDirectoryAry = OldDirectory.GetDirectories();
                        foreach ( DirectoryInfo aOldDirectory in OldDirectoryAry )
                        {
                                DirectoryInfo aNewDirectory = new DirectoryInfo(NewDirectoryFullName);
                                CopyDirectory(aOldDirectory, aNewDirectory);
                        }
                }

                //CopyDirectory
                public static void CopyDirectory(string OldDirectoryStr, string NewDirectoryStr)
                {
                        DirectoryInfo OldDirectory = new DirectoryInfo( OldDirectoryStr );
                        DirectoryInfo NewDirectory = new DirectoryInfo( NewDirectoryStr );       
                        FileExec.CopyDirectory(OldDirectory,NewDirectory);
                }

                //DelDirectory
                public static void DelDirectory(string OldDirectoryStr)
                {
                        DirectoryInfo OldDirectory = new DirectoryInfo( OldDirectoryStr );
                        OldDirectory.Delete(true);
                }

                //CopyDirectory And DelDirectory
                public static void CopyAndDelDirectory(string OldDirectory, string NewDirectory)
                {
                        CopyDirectory( OldDirectory,NewDirectory);
                        DelDirectory(  OldDirectory );
                }
        }
}

[ 本帖最后由 网事如风 于 2006-6-30 16:57 编辑 ]

TOP

很实用

TOP

发新话题