BadScript 2
Loading...
Searching...
No Matches
BadSystemFileSystem.cs
Go to the documentation of this file.
1namespace BadScript2.IO;
2
7{
8 public string GetStartupDirectory()
9 {
10 return AppDomain.CurrentDomain.BaseDirectory;
11 }
12
13 public bool Exists(string path)
14 {
15 return Directory.Exists(path) || File.Exists(path);
16 }
17
18 public bool IsFile(string path)
19 {
20 return File.Exists(path);
21 }
22
23 public bool IsDirectory(string path)
24 {
25 return Directory.Exists(path);
26 }
27
28 public IEnumerable<string> GetFiles(string path, string extension, bool recursive)
29 {
30 return Directory.GetFiles(
31 path,
32 $"*{extension}",
33 recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
34 );
35 }
36
37 public IEnumerable<string> GetDirectories(string path, bool recursive)
38 {
39 return Directory.GetDirectories(
40 path,
41 "*",
42 recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
43 );
44 }
45
46 public void CreateDirectory(string path, bool recursive = false)
47 {
48 List<string> directories = new List<string>();
49 string current = GetFullPath(path);
50
51 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
52 while (current != null)
53 {
54 directories.Add(current);
55 current = Path.GetDirectoryName(current)!;
56 }
57
58 for (int i = directories.Count - 1; i >= 0; i--)
59 {
60 if (!Directory.Exists(directories[i]))
61 {
62 Directory.CreateDirectory(directories[i]);
63 }
64 }
65 }
66
67 public void DeleteDirectory(string path, bool recursive)
68 {
69 Directory.Delete(path, recursive);
70 }
71
72 public void DeleteFile(string path)
73 {
74 File.Delete(path);
75 }
76
77 public string GetFullPath(string path)
78 {
79 return Path.GetFullPath(path);
80 }
81
82 public Stream OpenRead(string path)
83 {
84 return File.OpenRead(path);
85 }
86
87 public Stream OpenWrite(string path, BadWriteMode mode)
88 {
89 FileMode fileMode = mode switch
90 {
91 BadWriteMode.CreateNew => FileMode.Create,
92 BadWriteMode.Append => FileMode.Append,
93 _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null),
94 };
95
96 return File.Open(path, fileMode);
97 }
98
99 public string GetCurrentDirectory()
100 {
101 return Directory.GetCurrentDirectory();
102 }
103
104 public void SetCurrentDirectory(string path)
105 {
106 Directory.SetCurrentDirectory(path);
107 }
108
109 public void Copy(string src, string dst, bool overwrite = true)
110 {
111 if (File.Exists(src))
112 {
113 if (!overwrite && File.Exists(dst))
114 {
115 throw new IOException("File already exists");
116 }
117
118 File.Copy(src, dst, overwrite);
119 }
120 else if (Directory.Exists(src))
121 {
122 if (!overwrite && Directory.Exists(dst))
123 {
124 throw new IOException("Directory already exists");
125 }
126
127 foreach (string directory in Directory.GetDirectories(src, "*", SearchOption.AllDirectories))
128 {
129 Directory.CreateDirectory(directory.Replace(src, dst));
130 }
131
132 foreach (string file in Directory.GetFiles(src, "*", SearchOption.AllDirectories))
133 {
134 File.Copy(file, file.Replace(src, dst), overwrite);
135 }
136 }
137 }
138
139 public void Move(string src, string dst, bool overwrite = true)
140 {
141 if (File.Exists(src))
142 {
143 if (!overwrite && File.Exists(dst))
144 {
145 throw new IOException("File already exists");
146 }
147
148 File.Move(src, dst);
149 }
150 else if (Directory.Exists(src))
151 {
152 if (!overwrite && Directory.Exists(dst))
153 {
154 throw new IOException("Directory already exists");
155 }
156
157 Directory.Move(src, dst);
158 }
159 }
160}
Implements a wrapper for the actual OS file system.
bool IsFile(string path)
Returns true if the given path is a file.
Stream OpenRead(string path)
Opens a file for reading.
IEnumerable< string > GetDirectories(string path, bool recursive)
Returns all directories in the given directory.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
bool IsDirectory(string path)
Returns true if the given path is a directory.
void DeleteFile(string path)
Deletes a file.
Stream OpenWrite(string path, BadWriteMode mode)
Opens a file for writing.
bool Exists(string path)
Returns true if the given path is a file or directory.
void Copy(string src, string dst, bool overwrite=true)
Copies a file or directory to a new location.
void SetCurrentDirectory(string path)
Sets the current Directory.
string GetStartupDirectory()
The Startup Directory of the Application.
string GetCurrentDirectory()
Returns the Current Directory.
void Move(string src, string dst, bool overwrite=true)
Moves a file or directory to a new location.
string GetFullPath(string path)
Returns the full path of the given path.
void DeleteDirectory(string path, bool recursive)
Deletes a directory.
Defines the interface for a file system.
Definition IFileSystem.cs:7
Contains IO Implementation for the BadScript2 Runtime.
BadWriteMode
The Write Modes of the File System Abstraction.