-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetRes.cs
43 lines (39 loc) · 1.26 KB
/
GetRes.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
using System;
using System.IO;
using System.Reflection;
namespace RealAirplaneTag;
public class GetRes
{
public string GetFromJson(string filePath)
{
// 使用StreamReader读取文件
using (StreamReader reader = new StreamReader(filePath))
{
return reader.ReadToEnd();
}
}
public static string GetFromRes(string path)
{
string embeddedFilePath = path;
string fileContent = ReadEmbeddedFile(embeddedFilePath);
return fileContent;
}
static string ReadEmbeddedFile(string embeddedFilePath)
{
Type type = MethodBase.GetCurrentMethod().DeclaringType;
string _namespace = type.Namespace;
Assembly assembly = Assembly.GetExecutingAssembly();
//如果跨程序访问或者不确定文件,这里可以判断文件流是否为null
using (Stream stream = assembly.GetManifestResourceStream(_namespace+embeddedFilePath))
{
if (stream == null)
{
throw new Exception("Embedded文件未发现: " + embeddedFilePath);
}
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}