/ src / Ryujinx.Common / Utilities / FileSystemUtils.cs
FileSystemUtils.cs
 1  using System.Collections.Generic;
 2  using System.IO;
 3  using System.Linq;
 4  
 5  namespace Ryujinx.Common.Utilities
 6  {
 7      public static class FileSystemUtils
 8      {
 9          public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
10          {
11              // Get information about the source directory
12              var dir = new DirectoryInfo(sourceDir);
13  
14              // Check if the source directory exists
15              if (!dir.Exists)
16              {
17                  throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
18              }
19  
20              // Cache directories before we start copying
21              DirectoryInfo[] dirs = dir.GetDirectories();
22  
23              // Create the destination directory
24              Directory.CreateDirectory(destinationDir);
25  
26              // Get the files in the source directory and copy to the destination directory
27              foreach (FileInfo file in dir.GetFiles())
28              {
29                  string targetFilePath = Path.Combine(destinationDir, file.Name);
30                  file.CopyTo(targetFilePath);
31              }
32  
33              // If recursive and copying subdirectories, recursively call this method
34              if (recursive)
35              {
36                  foreach (DirectoryInfo subDir in dirs)
37                  {
38                      string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
39                      CopyDirectory(subDir.FullName, newDestinationDir, true);
40                  }
41              }
42          }
43  
44          public static void MoveDirectory(string sourceDir, string destinationDir)
45          {
46              CopyDirectory(sourceDir, destinationDir, true);
47              Directory.Delete(sourceDir, true);
48          }
49  
50          public static string SanitizeFileName(string fileName)
51          {
52              var reservedChars = new HashSet<char>(Path.GetInvalidFileNameChars());
53              return string.Concat(fileName.Select(c => reservedChars.Contains(c) ? '_' : c));
54          }
55      }
56  }