将文件夹数据复制到网络中的其他文件夹

时间:2014-02-06 14:55:29

标签: c# visual-studio-2010

我必须将文件夹和文件从一个网络文件夹复制到另一个网络文件夹。有些文件由于使用特殊字符命名,因此无法复制。 有太多文件夹和子文件夹,包含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

1 个答案:

答案 0 :(得分:1)

在这里,试试这个:

编辑:我的答案适用于本地网络目录,我没有提及,你想从HTTPS复制directiories,因为你必须使用带有凭据的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