## Embedded Resource 嵌入文件(推荐大量文档,txt/md/html)
### 步骤
1、把你的 `template.txt` 丢进类库项目里(可以建文件夹,比如 `Assets/template.txt`)
2、选中这个 txt 文件 → 属性窗口:
- **生成操作:嵌入的资源(Embedded Resource)**
- **复制到输出目录:不复制**
3、命名规则:
资源全名 = `项目默认命名空间.文件夹.文件名`
举例:
- 项目根命名空间:`MyTextLib`
- 文件路径:`Assets\demo.txt`
→ 资源名:`MyTextLib.Assets.demo.txt`
using System;
using System.IO;
using System.Reflection;
public static class ResourceHelper
{
///
/// 读取嵌入资源文本
///
/// 完整资源名称
/// 编码,默认UTF8
///
public static string ReadEmbeddedText(string resourceName, System.Text.Encoding encoding = null)
{
Assembly asm = Assembly.GetExecutingAssembly();
using (Stream stream = asm.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new FileNotFoundException("找不到嵌入资源:" + resourceName);
}
encoding = encoding ?? System.Text.Encoding.UTF8;
using (StreamReader sr = new StreamReader(stream, encoding))
{
return sr.ReadToEnd();
}
}
}
}