forked from ClearMeasure/AliaSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceFileLocator.cs
More file actions
66 lines (57 loc) · 2.03 KB
/
Copy pathResourceFileLocator.cs
File metadata and controls
66 lines (57 loc) · 2.03 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
using System;
using System.IO;
using System.Reflection;
using System.Text;
namespace AliaSQL.Core
{
public class ResourceFileLocator : IResourceFileLocator
{
public string ReadTextFile(string assembly, string resourceName)
{
using (Stream stream = getStream(assembly, resourceName))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string contents = reader.ReadToEnd();
return contents;
}
}
}
public byte[] ReadBinaryFile(string assembly, string resourceName)
{
using (Stream stream = getStream(assembly, resourceName))
{
using (BinaryReader reader = new BinaryReader(stream))
{
byte[] contents = reader.ReadBytes((int)stream.Length);
return contents;
}
}
}
public bool FileExists(string assembly, string resourceName)
{
Stream stream = constructStream(assembly, resourceName);
bool fileExists = stream != null;
return fileExists;
}
public Stream ReadFileAsStream(string assembly, string resourceName)
{
return getStream(assembly, resourceName);
}
private Stream getStream(string assembly, string resourceName)
{
Stream stream = constructStream(assembly, resourceName);
if (stream == null)
{
string template = "Resource file not found: {0}. Make sure the Build Action for the file is 'Embedded Resource'.";
throw new ApplicationException(string.Format(template, resourceName));
}
return stream;
}
private Stream constructStream(string assembly, string resourceName)
{
Stream stream = Assembly.Load(assembly).GetManifestResourceStream(resourceName);
return stream;
}
}
}