/ src / settings-ui / Settings.UI.Library / Utilities / SystemIOProvider.cs
SystemIOProvider.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.IO.Abstractions;
 7  
 8  namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
 9  {
10      public class SystemIOProvider : IIOProvider
11      {
12          private readonly IDirectory _directory;
13          private readonly IFile _file;
14  
15          public SystemIOProvider()
16              : this(new FileSystem())
17          {
18          }
19  
20          public SystemIOProvider(IFileSystem fileSystem)
21              : this(fileSystem?.Directory, fileSystem?.File)
22          {
23          }
24  
25          private SystemIOProvider(IDirectory directory, IFile file)
26          {
27              _directory = directory ?? throw new ArgumentNullException(nameof(directory));
28              _file = file ?? throw new ArgumentNullException(nameof(file));
29          }
30  
31          public bool CreateDirectory(string path)
32          {
33              var directoryInfo = _directory.CreateDirectory(path);
34              return directoryInfo != null;
35          }
36  
37          public void DeleteDirectory(string path)
38          {
39              _directory.Delete(path, recursive: true);
40          }
41  
42          public bool DirectoryExists(string path)
43          {
44              return _directory.Exists(path);
45          }
46  
47          public bool FileExists(string path)
48          {
49              return _file.Exists(path);
50          }
51  
52          public string ReadAllText(string path)
53          {
54              return _file.ReadAllText(path);
55          }
56  
57          public void WriteAllText(string path, string content)
58          {
59              _file.WriteAllText(path, content);
60          }
61      }
62  }