forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cs
More file actions
141 lines (113 loc) · 3.48 KB
/
FileSystem.cs
File metadata and controls
141 lines (113 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.IO;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class FileSystem : IFileSystem
{
public IEnumerable<string> EnumerateFiles(string dir, string searchPattern, SearchOption searchOption = SearchOption.AllDirectories)
{
return Directory.EnumerateFiles(dir, searchPattern, searchOption);
}
public void Copy(string source, string dest, bool overwrite)
{
File.Copy(source, dest, overwrite);
}
public bool DirectoryExists(string path)
{
return Directory.Exists(path);
}
public void CreateDirectory(string path)
{
Directory.CreateDirectory(path);
}
public void DeleteDirectory(string path)
{
Directory.Delete(path, true);
}
public string ReadFile(string path)
{
return File.ReadAllText(path);
}
public string[] ReadFileLines(string path)
{
return File.ReadAllLines(path);
}
public bool IsPathRooted(string path)
{
return Path.IsPathRooted(path);
}
public string CurrentDirectory
{
get { return Environment.CurrentDirectory; }
set { Environment.CurrentDirectory = value; }
}
public string NewLine
{
get { return Environment.NewLine; }
}
public DateTime GetLastWriteTime(string file)
{
return File.GetLastWriteTime(file);
}
public void Move(string source, string dest)
{
File.Move(source, dest);
}
public bool FileExists(string path)
{
return File.Exists(path);
}
public void FileDelete(string path)
{
File.Delete(path);
}
public IEnumerable<string> SplitLines(string value)
{
Guard.AgainstNullArgument("value", value);
return value.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
}
public void WriteToFile(string path, string text)
{
File.WriteAllText(path, text);
}
public Stream CreateFileStream(string filePath, FileMode mode)
{
return new FileStream(filePath, mode);
}
public void WriteAllBytes(string filePath, byte[] bytes)
{
File.WriteAllBytes(filePath, bytes);
}
public string ModulesFolder
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "scriptcs");
}
}
public string GetWorkingDirectory(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return CurrentDirectory;
}
var realPath = GetFullPath(path);
if (FileExists(realPath) || DirectoryExists(realPath))
{
var attributes = File.GetAttributes(realPath);
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
return realPath;
}
return Path.GetDirectoryName(realPath);
}
return Path.GetDirectoryName(realPath);
}
public string GetFullPath(string path)
{
return Path.GetFullPath(path);
}
}
}