【问题标题】:Copy folder data to other folder in network将文件夹数据复制到网络中的其他文件夹
【发布时间】:2014-02-06 23:13:55
【问题描述】:

我必须将文件夹和文件从一个网络文件夹复制到另一个。有一些文件不能被复制,因为它是用特殊字符命名的。 有很多文件夹和子文件夹包含 3 GB 的数据。为了避免这个问题,我想编写一个可以复制所有文件夹、子文件夹和文件的 C# 程序 带有日志文件(记事本)。日志文件,其中应注意非复制文件的详细信息及其路径,以便进一步跟踪它们。任何人都可以快速帮助我吗 通过提供一个 c# 程序或至少一个参考。控制台或 Win-form 应用程序,我使用 Visual Studio 2010 和 Windows 7

复制如下

复制表格:- https://ap.sharepoint.a5-group.com/cm/Shared Documents/IRD/EA

收件人:- https://cr.sp.a5-group.com/sites/cm/Shared Documents/IRD/EA

【问题讨论】:

  • 您尝试过任何代码吗?您似乎希望将文件从源递归复制到目标。

标签: c# visual-studio-2010


【解决方案1】:

在这里,试试这个:

编辑:我的答案适用于本地网络目录,我没有提到,你想从 HTTPS 复制目录,因为你必须使用带有凭据的 WebClient

class DirectoryCopyExample
    {
        string pathFrom = "C:\\someFolder";
        string pathTo = "D:\\otherFolder";
        string LogText = string.Empty;
        static void Main()
        {
            // Copy from the current directory, include subdirectories.
            DirectoryCopy(pathFrom, pathTo, true);
        }

        private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            // If the destination directory doesn't exist, create it. 
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                try
                {
                    string temppath = Path.Combine(destDirName, file.Name);
                    file.CopyTo(temppath, false);
                }
                catch(Exception)
                {
                    //Write Files to Log whicht couldn't be copy
                    LogText += DateTime.Now.ToString() + ": " + file.FullName;
                }
            }

            // If copying subdirectories, copy them and their contents to new location. 
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
    }

最后你必须将变量 LogTex 保存到文件中,或者你需要的任何东西

来源:http://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

【讨论】:

  • 非常感谢Magix,它很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2021-05-17
  • 2012-08-17
  • 2019-03-02
  • 1970-01-01
  • 2019-05-05
相关资源
最近更新 更多