65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET 目录复制程序

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.56/5 (7投票s)

2005年6月24日

viewsIcon

45671

downloadIcon

610

本文填补了.NET框架中复制目录内容的空白。

引言

这是我在CodeProject上的第一篇投稿。 提供的代码用于复制文件夹中的所有内容,如果需要,还可以在过程失败后尝试将其复制到其他地方。

背景

Microsoft .NET框架没有提供复制目录的功能。 本文和随附的代码解决了这个问题。

使用代码

核心在于这个函数,它能整齐地搜索目录和文件,然后在创建定义表后将它们复制到新目录中。

public void DirectorySearch(string strSourceDirectory, 
             string strSourceDirectoryConstant, 
             string strDestinationDirectory)
{
    iNoOfDir++;
    string [] strarrDirectoryNames= Directory.GetDirectories(strSourceDirectory);
    string [] strarrFileNames= Directory.GetFiles(strSourceDirectory);
    foreach(string strFileName in strarrFileNames)
    {
        try
        {
            iNoOfFiles++;
            FileInfo info=new FileInfo(strFileName);
            long fileLength= info.Length;
            totalLength =totalLength+fileLength;
            //Below the function Insert is used which 
            //is found in the code : purpose to store the data
            Insert("FILE", strFileName, strSourceDirectoryConstant + @"\" + 
                   info.Name, fileLength.ToString(), strDestinationDirectory);
            info=null;
        }
        catch
        {
        }
    }
    foreach(string strDirectoryName in strarrDirectoryNames)
    {
        DirectoryInfo info=new DirectoryInfo(strDirectoryName);
        DirectorySearch(strDirectoryName, strSourceDirectoryConstant 
                        + @"\" + info.Name, strDestinationDirectory);
        Insert("DIRECTORY", strDirectoryName, strSourceDirectoryConstant 
               + @"\" + info.Name, "", strDestinationDirectory);
        info=null;
    }
}

历史

  • 版本 1.0 - 2005年6月20日 下午5:19。
© . All rights reserved.