Repository navigation
Expand file tree
/
Copy pathFileSystem.cs
More file actions
88 lines (72 loc) · 2.45 KB
/
Copy pathFileSystem.cs
File metadata and controls
88 lines (72 loc) · 2.45 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
using System;
using System.IO;
namespace AliaSQL.Core
{
public class FileSystem : IFileSystem
{
private readonly IFileStreamFactory _streamFactory;
public FileSystem(IFileStreamFactory streamFactory)
{
_streamFactory = streamFactory;
}
public FileSystem():this(new FileStreamFactory())
{
}
public void SaveFile(string filename, byte[] fileContent)
{
if (fileContent != null)
{
var fileStream = _streamFactory.ConstructWriteFileStream(filename);
using (var writer = new BinaryWriter(fileStream))
{
writer.Write(fileContent);
}
}
}
public bool FileExists(string relativePath)
{
var retval = File.Exists(relativePath);
return retval;
}
public Stream ReadIntoFileStream(string path)
{
try
{
var stream = _streamFactory.ConstructReadFileStream(path);
return stream;
}
catch (IOException ex)
{
if (ex.Message.IndexOf("it is being used by another process") >= 0)
{
throw new ApplicationException("The file you chose cannot be read because it is open in another application. Please close the file in the other application and try again.");
}
throw;
}
}
public string[] GetAllFilesWithExtensionWithinFolder(string folder, string fileExtension)
{
var fileNames = new string[0];
if (Directory.Exists(folder))
{
var searchPattern = string.Format("*.{0}", fileExtension);
fileNames = Directory.GetFiles(folder, searchPattern, SearchOption.AllDirectories);
}
return fileNames;
}
public string ReadTextFile(string filename)
{
var stream = _streamFactory.ConstructReadFileStream(filename);
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
public StreamReader ReadFileIntoStreamReader(string filename)
{
var stream = _streamFactory.ConstructReadFileStream(filename);
var reader = new StreamReader(stream);
return reader;
}
}
}