-
Notifications
You must be signed in to change notification settings - Fork 225
/
DefaultFileSystem.cs
79 lines (66 loc) · 2.22 KB
/
DefaultFileSystem.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.VisualStudio.Web.CodeGeneration
{
/// <summary>
/// Default implementation of <see cref="IFileSystem"/>
/// using the real file sytem.
/// </summary>
public class DefaultFileSystem : IFileSystem
{
public static DefaultFileSystem Instance = new DefaultFileSystem();
public async Task AddFileAsync(string outputPath, Stream sourceStream)
{
using (var writeStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
{
await sourceStream.CopyToAsync(writeStream);
}
}
public void CreateDirectory(string path)
{
Directory.CreateDirectory(path);
}
public void DeleteFile(string path)
{
File.Delete(path);
}
public bool DirectoryExists(string path)
{
return Directory.Exists(path);
}
public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
{
return Directory.EnumerateFiles(path, searchPattern, searchOption);
}
public bool FileExists(string path)
{
return File.Exists(path);
}
public void MakeFileWritable(string path)
{
Debug.Assert(File.Exists(path));
FileAttributes attributes = File.GetAttributes(path);
if (attributes.HasFlag(FileAttributes.ReadOnly))
{
File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly);
}
}
public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
public void RemoveDirectory(string path, bool recursive)
{
Directory.Delete(path, recursive);
}
public void WriteAllText(string path, string contents)
{
File.WriteAllText(path, contents);
}
}
}