C#.NET实现文件夹的复制

 
      .NET只提供了文件的Copy方法,但却没有提供文件夹的Copy方法,经过一天的努力,终于搞定.现贴出主要代码供大家参考,欢迎大家提出建议. 代码如下:         public int DirectoryName(string DirectoryPath)//获取文件夹名,截取“/”
        {
        int j = 0;     char[] c = DirectoryPath.ToCharArray();
         for (int i = c.Length - 1; i >= 0; i--)//从后面截取
         {
        j = i;
        if (c[i] == '//')
         {
        break;//遇"/"调处,并返回"/"的位置
         }
        }
        return j+1;
        }
        public void CopyDirectory(string DirectoryPath,string DirAddress)//复制文件夹,
         {
            #region//递归
            string s = DirectoryPath.Substring(DirectoryName(DirectoryPath));//获取文件夹名
             if (Directory.Exists(DirAddress + "//" + s))
            {
                Directory.Delete(DirAddress + "//" + s,true);//若文件夹存在,不管目录是否为空,删除
                 Directory.CreateDirectory(DirAddress + "//" + s);//删除后,重新创建文件夹
            }
            else
             {
                 Directory.CreateDirectory(DirAddress + "//" + s);//文件夹不存在,创建
            }
            DirectoryInfo DirectoryArray = new DirectoryInfo(DirectoryPath);
            FileInfo[] Files = DirectoryArray.GetFiles();//获取该文件夹下的文件列表
            DirectoryInfo[] Directorys = DirectoryArray.GetDirectories();//获取该文件夹下的文件夹列表
            foreach (FileInfo inf in Files)//逐个复制文件
            {
                File.Copy(DirectoryPath + "//" + inf.Name, DirAddress + "//" + s + "//" + inf.Name);
            }
            foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称,并递归调用方法本身
            {
                CopyDirectory(DirectoryPath + "//" + Dir.Name, DirAddress + "//" + s);
            }
            #endregion
        }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flysun0311/archive/2008/08/19/2797364.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#中的System.IO和System.Net命名空间来实现复制本地文件夹到共享文件夹的功能。以下是实现的基本步骤: 1. 使用Directory.GetFiles和Directory.GetDirectories方法获取本地文件夹中的文件和子文件夹。 2. 使用File.Copy方法将文件从本地文件夹复制到共享文件夹。 3. 使用Directory.CreateDirectory方法在共享文件夹中创建子文件夹。 4. 递归调用自己,将子文件夹中的文件和文件夹复制到共享文件夹中。 以下是一个示例代码,可以将本地文件夹D:\LocalFolder复制到共享文件夹\\Server\SharedFolder: ```csharp using System.IO; using System.Net; public void CopyFolderToSharedFolder() { string localFolderPath = @"D:\LocalFolder"; string sharedFolderPath = @"\\Server\SharedFolder"; // Create a WebRequest instance for the shared folder WebRequest sharedFolderRequest = WebRequest.Create(sharedFolderPath); NetworkCredential credentials = new NetworkCredential("username", "password"); sharedFolderRequest.Credentials = credentials; // Get the response from the shared folder request WebResponse sharedFolderResponse = sharedFolderRequest.GetResponse(); // Get the shared folder stream and create a StreamWriter instance Stream sharedFolderStream = sharedFolderResponse.GetResponseStream(); StreamWriter sharedFolderWriter = new StreamWriter(sharedFolderStream); // Get the files and directories in the local folder string[] files = Directory.GetFiles(localFolderPath); string[] directories = Directory.GetDirectories(localFolderPath); // Copy the files to the shared folder foreach (string file in files) { // Get the filename from the file path string filename = Path.GetFileName(file); // Create the shared file path string sharedFilePath = Path.Combine(sharedFolderPath, filename); // Copy the file to the shared folder File.Copy(file, sharedFilePath); } // Copy the directories to the shared folder foreach (string directory in directories) { // Get the directory name from the directory path string directoryName = Path.GetFileName(directory); // Create the shared directory path string sharedDirectoryPath = Path.Combine(sharedFolderPath, directoryName); // Create the shared directory Directory.CreateDirectory(sharedDirectoryPath); // Recursively copy the files and directories in the local directory to the shared directory CopyFolder(directory, sharedDirectoryPath); } // Close the shared folder stream and response sharedFolderWriter.Close(); sharedFolderResponse.Close(); } private void CopyFolder(string sourceFolderPath, string targetFolderPath) { // Get the files and directories in the source folder string[] files = Directory.GetFiles(sourceFolderPath); string[] directories = Directory.GetDirectories(sourceFolderPath); // Copy the files to the target folder foreach (string file in files) { // Get the filename from the file path string filename = Path.GetFileName(file); // Create the target file path string targetFilePath = Path.Combine(targetFolderPath, filename); // Copy the file to the target folder File.Copy(file, targetFilePath); } // Copy the directories to the target folder foreach (string directory in directories) { // Get the directory name from the directory path string directoryName = Path.GetFileName(directory); // Create the target directory path string targetDirectoryPath = Path.Combine(targetFolderPath, directoryName); // Create the target directory Directory.CreateDirectory(targetDirectoryPath); // Recursively copy the files and directories in the source directory to the target directory CopyFolder(directory, targetDirectoryPath); } } ``` 在上面的代码中,我们首先使用WebRequest和NetworkCredential类设置访问共享文件夹所需的凭据。然后,我们使用Directory.GetFiles和Directory.GetDirectories方法获取本地文件夹中的文件和子文件夹,并使用File.Copy方法将文件从本地文件夹复制到共享文件夹。接下来,我们使用Directory.CreateDirectory方法在共享文件夹中创建子文件夹,并递归调用自己,将子文件夹中的文件和文件夹复制到共享文件夹中。最后,我们关闭共享文件夹流和响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值