直接上代码
1 2 3 4 5 6 7 8 9
| public string ReadEmbeddedFile(string fileName) { using (var stream = this.GetType().Assembly.GetManifestResourceStream(fileName)) { byte[] b = new byte[stream.Length]; stream.Read(b, 0, b.Length); return Encoding.UTF8.GetString(b); } }
|
会有疑问,文件长啥样子,怎么得出来的,可以通过this.GetType().Assembly.GetManifestResourceNames()
来获取当前程序集下面的嵌入式文件。多提一句嵌入式文件可以跟Nuget包一起生成。