TempDirectory.cs (3920B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="TempDirectory.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Helpers 8 { 9 using System; 10 using System.IO; 11 12 /// <summary> 13 /// Creates a temporary directory in the user's temporary directory. 14 /// </summary> 15 internal class TempDirectory : IDisposable 16 { 17 private readonly bool cleanup; 18 private bool disposed = false; 19 20 /// <summary> 21 /// Initializes a new instance of the <see cref="TempDirectory"/> class. 22 /// </summary> 23 /// <param name="directoryName">Optional directory name. If null, creates a random directory name.</param> 24 /// <param name="deleteIfExists">Delete directory if already exists. Default true.</param> 25 /// <param name="cleanup">Deletes directory at disposing time. Default true.</param> 26 public TempDirectory( 27 string? directoryName = null, 28 bool deleteIfExists = true, 29 bool cleanup = true) 30 { 31 if (directoryName is null) 32 { 33 this.DirectoryName = Path.GetRandomFileName(); 34 } 35 else 36 { 37 this.DirectoryName = directoryName; 38 } 39 40 this.FullDirectoryPath = Path.Combine(Path.GetTempPath(), this.DirectoryName); 41 42 if (deleteIfExists && Directory.Exists(this.FullDirectoryPath)) 43 { 44 Directory.Delete(this.FullDirectoryPath, true); 45 } 46 47 Directory.CreateDirectory(this.FullDirectoryPath); 48 this.cleanup = cleanup; 49 } 50 51 /// <summary> 52 /// Gets the directory name. 53 /// </summary> 54 public string DirectoryName { get; } 55 56 /// <summary> 57 /// Gets the full directory name. 58 /// </summary> 59 public string FullDirectoryPath { get; } 60 61 /// <summary> 62 /// IDisposable.Dispose . 63 /// </summary> 64 public void Dispose() 65 { 66 this.Dispose(true); 67 GC.SuppressFinalize(this); 68 } 69 70 /// <summary> 71 /// Copies all contents of a directory into this directory. 72 /// </summary> 73 /// <param name="sourceDir">Source directory.</param> 74 public void CopyDirectory(string sourceDir) 75 { 76 this.CopyDirectory(sourceDir, this.FullDirectoryPath); 77 } 78 79 /// <summary> 80 /// Protected disposed. 81 /// </summary> 82 /// <param name="disposing">Disposing.</param> 83 protected virtual void Dispose(bool disposing) 84 { 85 if (!this.disposed) 86 { 87 if (this.cleanup && Directory.Exists(this.FullDirectoryPath)) 88 { 89 Directory.Delete(this.FullDirectoryPath, true); 90 } 91 92 this.disposed = true; 93 } 94 } 95 96 private void CopyDirectory(string sourceDir, string destinationDir) 97 { 98 var dir = new DirectoryInfo(sourceDir); 99 100 if (!dir.Exists) 101 { 102 throw new DirectoryNotFoundException(dir.FullName); 103 } 104 105 Directory.CreateDirectory(destinationDir); 106 107 foreach (FileInfo file in dir.GetFiles()) 108 { 109 file.CopyTo(Path.Combine(destinationDir, file.Name)); 110 } 111 112 foreach (DirectoryInfo subDir in dir.GetDirectories()) 113 { 114 this.CopyDirectory(subDir.FullName, Path.Combine(destinationDir, subDir.Name)); 115 } 116 } 117 } 118 }