c# 读取嵌入式文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace zhang_Test
{
class Program
{
private static void Main(string[] args)
{


InitSchemes dd = new InitSchemes();
string ddds = dd.GetInitSchemes(null);
}

}
/// <summary>
/// 出厂原始数据
/// </summary>
public class InitSchemes
{
public string GetInitSchemes(Assembly resourceAssembly = null, string resourceName = "zhang_Test.test.txt")
{
if (resourceAssembly == null)
{
resourceAssembly = this.GetType().Assembly;
}

string json = resourceAssembly.Resource(resourceName);


return json;
}
}
public static class AssemblyExtensions
{
public static string Resource(this Assembly self, string name, Encoding encoding = null)
{

var stream = self.GetManifestResourceStream(name);
if (stream == null)
{
return null;
}

if (!stream.CanRead)
{
return string.Empty;
}

//结果
var result = string.Empty;
using (var streamReader = encoding != null ? new StreamReader(stream, encoding) : new StreamReader(stream, Encoding.UTF8))
{
result = streamReader.ReadToEnd();
}

return result;
}
}
}

原文地址:https://www.cnblogs.com/zhtbk/p/4692295.html