C#应用程序无法加载嵌入的文本资源
C# application can not load embedded text resources
我无法读取嵌入的资源文本文件。
我在启动 C# 应用程序时不断收到此错误
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: stream
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding)
at abcdefg.abcdefgh.LoadResource(String ResourcePath)
at abcdefg.abcdefgh.<Main>b__0()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这是方法:
public static string LoadResource(string ResourcePath)
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(ResourcePath))
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
这就是我使用 CODEDOM 编译可执行文件的方式:
Dictionary<string, string> provOptions = new Dictionary<string, string>();
provOptions.Add("CompilerVersion", "v4.0");
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp", provOptions);
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.Net.dll");
parameters.ReferencedAssemblies.Add("System.Windows.dll");
parameters.ReferencedAssemblies.Add("System.Net.Http.dll");
parameters.ReferencedAssemblies.Add("System.Management.dll");
parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
parameters.ReferencedAssemblies.Add("System.Deployment.dll");
parameters.ReferencedAssemblies.Add("System.Xml.Linq.dll");
parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
var assemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location);
parameters.ReferencedAssemblies.AddRange(assemblies.ToArray());
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
parameters.CompilerOptions = "/utf8output /warnaserror- /debug- /optimize+ /unsafe+ /target:exe /platform:x86";
parameters.TreatWarningsAsErrors = false;
parameters.EmbeddedResources.Add(resfile);
CompilerResults results = provider.CompileAssemblyFromSource(parameters, mainScript);
我使用的资源路径格式是:abcdefg.Resource1.txt
我使用这段代码修复了它,它循环遍历我想要的所有资源和选择:
string[] resNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (string resName in resNames)
{
if (resName == ("Resource1.txt"))
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourcename = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resName));
Stream stream = assembly.GetManifestResourceStream(resourcename);
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string output = reader.ReadToEnd();
reader.Dispose();
stream.Dispose();
}
}
我无法读取嵌入的资源文本文件。 我在启动 C# 应用程序时不断收到此错误
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: stream
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding)
at abcdefg.abcdefgh.LoadResource(String ResourcePath)
at abcdefg.abcdefgh.<Main>b__0()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这是方法:
public static string LoadResource(string ResourcePath)
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(ResourcePath))
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
这就是我使用 CODEDOM 编译可执行文件的方式:
Dictionary<string, string> provOptions = new Dictionary<string, string>();
provOptions.Add("CompilerVersion", "v4.0");
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp", provOptions);
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.Net.dll");
parameters.ReferencedAssemblies.Add("System.Windows.dll");
parameters.ReferencedAssemblies.Add("System.Net.Http.dll");
parameters.ReferencedAssemblies.Add("System.Management.dll");
parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
parameters.ReferencedAssemblies.Add("System.Deployment.dll");
parameters.ReferencedAssemblies.Add("System.Xml.Linq.dll");
parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
var assemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location);
parameters.ReferencedAssemblies.AddRange(assemblies.ToArray());
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
parameters.CompilerOptions = "/utf8output /warnaserror- /debug- /optimize+ /unsafe+ /target:exe /platform:x86";
parameters.TreatWarningsAsErrors = false;
parameters.EmbeddedResources.Add(resfile);
CompilerResults results = provider.CompileAssemblyFromSource(parameters, mainScript);
我使用的资源路径格式是:abcdefg.Resource1.txt
我使用这段代码修复了它,它循环遍历我想要的所有资源和选择:
string[] resNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (string resName in resNames)
{
if (resName == ("Resource1.txt"))
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourcename = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resName));
Stream stream = assembly.GetManifestResourceStream(resourcename);
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string output = reader.ReadToEnd();
reader.Dispose();
stream.Dispose();
}
}