Why does Directory.CreateDirectory sometimes fail?

Here is my code that I use to extract the zip file, making sure there are no dirty files in the destination directory.

internal void UnzipProject()
{
    if (Directory.Exists(SourceDir))
        Directory.Delete(SourceDir, true);

    if (File.Exists(CodeZipFile))
    {
        Directory.CreateDirectory(SourceDir); // fails here
        ZipFile.ExtractToDirectory(CodeZipFile, SourceDir);
    }
}

Once it was Directory.CreateDirectory(SourceDir)not possible to create a new dir, and I get an exception in the next line, but if I backtrack and try to create a dir again, it will work. Exactly the same pattern is repeated on the next run.

EDIT
Here is an exception that is actually related to the fact that dir was not created, I see that src dir does not exist:

System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=Access to the path '(...MyPath...)\src\MySolution.sln' is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
       at System.IO.Compression.ZipFileExtensions.ExtractToFile(ZipArchiveEntry source, String destinationFileName, Boolean overwrite)
       at System.IO.Compression.ZipFileExtensions.ExtractToDirectory(ZipArchive source, String destinationDirectoryName)
       at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding)
       at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName)
........
+4
source share
5 answers

, . . , , , - , . , :

internal void UnzipProject()
{
    if (Directory.Exists(SourceDir))
    {
        DirectoryInfo di = new DirectoryInfo(SourceDir);

        foreach (FileInfo file in di.GetFiles())
            file.Delete();

        foreach (DirectoryInfo dir in di.GetDirectories())
            dir.Delete(true);
    }

    if (File.Exists(zipFile))
        ZipFile.ExtractToDirectory(zipFile, SourceDir);

    else
    {
        if (Directory.Exists(SourceDir))
            Directory.Delete(SourceDir, true);
    }
}
+2

Windows 7,.Net 4.0, VS2010. , Directory.Delete() , . , Directory.CreateDirectory() , . "Directory.Delete()).

, :

if (Directory.Exists(myPath))
  {
  Directory.Delete(myPath, true);
  while (Directory.Exists(myPath))
    System.Threading.Thread.Sleep(10);
  }

Directory.CreateDirectory(myPath);
+8

.

System.UnauthorizedAccessException was unhandled

/ , .

+4

. , Fyle . , , .

UnauthorizedAccessException \ Thread.Sleep(100) Directory.Delete.

0

. , , Directory.Delete , , Directory.CreateDirectory . CreateDirectory , , .

I avoided this problem by introducing a slight delay between calling Delete and calling CreateDirectory. Another option that I remember was to rename the directory and then delete it. For example, if the source directory was called by MyApp, I would rename it to DeleteMe and then delete that directory. CreateDirectory should be able to create the MyApp directory as this no longer exists.

0
source

Source: https://habr.com/ru/post/1626419/


All Articles