在C#中创建文件夹 - Windows认为它既是文件又是文件夹

时间:2016-01-14 20:15:05

标签: c# .net window system.io.directory

我有一个随机的问题,Windows认为文件夹实际上是一个文件和文件夹。它发生在随机机器上,我无法复制它。我正在运行.NET Directory.CreateDirectory方法。

我基本上运行这段代码:

string directoryToCreate = folderPath + @"\" + directoryName + @"\";
 if (Directory.Exists(directoryToCreate ) == false)
         Directory.CreateDirectory(directoryToCreate);

创建目录后,我会将一堆文件复制到它。

文件夹/文件在资源管理器中显示为“文件”类型,但您无法创建具有相同名称的其他文件或文件夹。我已经尝试了使用和不使用反斜杠的代码,它没有什么区别

在创建目录后立即调用:

    private static void ProcessDirectoryForUpdating(string pNetworkFolder, string pLocalFolder)
    {
        DirectoryInfo networkFolder = new DirectoryInfo(pNetworkFolder);
        DirectoryInfo localFolder = new DirectoryInfo(pLocalFolder);

        try
        {
            foreach (DirectoryInfo tmpNetworkFolder in networkFolder.GetDirectories())
            {
                if (Directory.Exists(localFolder.FullName + @"\" + tmpNetworkFolder.Name) == false)
                    Directory.CreateDirectory(localFolder.FullName + @"\" + tmpNetworkFolder.Name);

                ProcessDirectoryForUpdating(tmpNetworkFolder.FullName, localFolder.FullName + @"\" + tmpNetworkFolder.Name);
            }
        }
        catch (Exception ex)
        {
            tracer.TraceEvent(TraceEventType.Error, 0, "ProcessDirectoryForUpdating:  Error processing directory " + pNetworkFolder + "  " + ex.Message);
        }

        foreach (FileInfo tmpNetworkFile in networkFolder.GetFiles())
        {
            try
            {
                if (File.Exists(localFolder.FullName + @"\" + tmpNetworkFile.Name) == false)
                    tmpNetworkFile.CopyTo(localFolder.FullName + @"\" + tmpNetworkFile.Name, true);
                else if (tmpNetworkFile.LastWriteTime > (new DirectoryInfo(localFolder.FullName + @"\" + tmpNetworkFile.Name).LastWriteTime))
                    tmpNetworkFile.CopyTo(localFolder.FullName + @"\" + tmpNetworkFile.Name, true);

            }
            catch (Exception ex)
            {
                tracer.TraceEvent(TraceEventType.Error, 0, "ProcessDirectoryForUpdating:  Error processing file " + tmpNetworkFile.FullName + "  " + ex.Message);
            }
        }

    }

0 个答案:

没有答案