Assembly.GetExecutingAssembly()的问题 [英] Problem With Assembly.GetExecutingAssembly()

查看:157
本文介绍了Assembly.GetExecutingAssembly()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的代码中尝试使用以下功能:



Hi,
I am trying using below function in my code:

var assembly = Assembly.GetExecutingAssembly();
            var resourceName = "C://reza.txt";
            string result = "";
            
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(stream))
            {
                 result = reader.ReadToEnd();
              
            }





但对于此部分:new StreamReader(stream))

我得到以下错误:





But for this section: "new StreamReader(stream))
I get below errors:

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: stream
  Source=mscorlib
  ParamName=stream
  StackTrace:
       at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
       at System.IO.StreamReader..ctor(Stream stream)
       at SendEmail.Program.Email(String ename, String name) in C:\Users\Reza\Desktop\Test_Projects\C#\SendEmail\SendEmail\Program.cs:line 44
       at SendEmail.Program.Main(String[] args) in C:\Users\Reza\Desktop\Test_Projects\C#\SendEmail\SendEmail\Program.cs:line 21
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:





如何解决这个问题?

问候,



How to fix that?
Regards,

推荐答案

尝试从资源名称中删除c:/。
Try removing the "c:/" from your resource name.


在Visual Studio的解决方案资源管理器中,右键单击该项,选择属性。将Bu​​ild Action设置为Embedded resource。然后重新构建您的应用程序。
In Visual Studio's solution explorer, right click on that item, select properties. Set "Build Action" to "Embedded resource". Then re-build your application.


这篇关于Assembly.GetExecutingAssembly()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆