写入USB时C#IOException

时间:2015-01-23 04:16:43

标签: c#

我正在编写大量文件(确切地说是80,000个),这些文件位于我的硬盘驱动器上并将它们复制到我的闪存驱动器中。事情开始没问题,但在第29,648个文件中,我收到一条IOException,声明The directory or file cannot be created.

我曾经通过互联网找到了不同的目录复制方式:

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

Copy the entire contents of a directory in C#

他们都以相同的结果结束了。

关于它为何失败的任何想法?闪存驱动器有足够的空间,我知道该文件不重复,因为我开始使用空白闪存驱动器。

class Program
    {
        static void Main(string[] args)
        {            
            DirectoryCopy(Directory.GetCurrentDirectory(), "F://", true);
        }

        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)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            // 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);
                }
            }
        }
}

0 个答案:

没有答案