winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

TempDirectory.cs (3971B)


      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.Management.Configuration.UnitTests.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 bool disposed = false;
     18         private bool cleanup;
     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             var path = Path.GetTempPath();
     32 
     33             if (directoryName is null)
     34             {
     35                 this.DirectoryName = Path.GetRandomFileName();
     36             }
     37             else
     38             {
     39                 this.DirectoryName = directoryName;
     40             }
     41 
     42             this.FullDirectoryPath = Path.Combine(Path.GetTempPath(), this.DirectoryName);
     43 
     44             if (deleteIfExists && Directory.Exists(this.FullDirectoryPath))
     45             {
     46                 Directory.Delete(this.FullDirectoryPath, true);
     47             }
     48 
     49             Directory.CreateDirectory(this.FullDirectoryPath);
     50             this.cleanup = cleanup;
     51         }
     52 
     53         /// <summary>
     54         /// Gets the directory name.
     55         /// </summary>
     56         public string DirectoryName { get; }
     57 
     58         /// <summary>
     59         /// Gets the full directory name.
     60         /// </summary>
     61         public string FullDirectoryPath { get; }
     62 
     63         /// <summary>
     64         /// IDisposable.Dispose .
     65         /// </summary>
     66         public void Dispose()
     67         {
     68             this.Dispose(true);
     69             GC.SuppressFinalize(this);
     70         }
     71 
     72         /// <summary>
     73         /// Copies all contents of a directory into this directory.
     74         /// </summary>
     75         /// <param name="sourceDir">Source directory.</param>
     76         public void CopyDirectory(string sourceDir)
     77         {
     78             this.CopyDirectory(sourceDir, this.FullDirectoryPath);
     79         }
     80 
     81         /// <summary>
     82         /// Protected disposed.
     83         /// </summary>
     84         /// <param name="disposing">Disposing.</param>
     85         protected virtual void Dispose(bool disposing)
     86         {
     87             if (!this.disposed)
     88             {
     89                 if (this.cleanup && Directory.Exists(this.FullDirectoryPath))
     90                 {
     91                     Directory.Delete(this.FullDirectoryPath, true);
     92                 }
     93 
     94                 this.disposed = true;
     95             }
     96         }
     97 
     98         private void CopyDirectory(string sourceDir, string destinationDir)
     99         {
    100             var dir = new DirectoryInfo(sourceDir);
    101 
    102             if (!dir.Exists)
    103             {
    104                 throw new DirectoryNotFoundException(dir.FullName);
    105             }
    106 
    107             Directory.CreateDirectory(destinationDir);
    108 
    109             foreach (FileInfo file in dir.GetFiles())
    110             {
    111                 file.CopyTo(Path.Combine(destinationDir, file.Name));
    112             }
    113 
    114             foreach (DirectoryInfo subDir in dir.GetDirectories())
    115             {
    116                 this.CopyDirectory(subDir.FullName, Path.Combine(destinationDir, subDir.Name));
    117             }
    118         }
    119     }
    120 }