Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying using below function in my code:

C#
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();
              
            }


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

XML
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,
Posted
Comments
[no name] 28-Aug-13 13:46pm    
You need to check the number of braces that you have for your using statements.... hint hint.
Rezame 28-Aug-13 13:49pm    
When compile script not shows any error.
but when try run I get this error.
[no name] 28-Aug-13 14:01pm    
Yes... and? That is because stream is null. You are defining stream in one using block then it goes out of scope because you are missing the braces that define that using block, then you are trying to use a null object in another using block.
Rezame 28-Aug-13 14:06pm    
Trying define as below:
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
But same error.
OriginalGriff 28-Aug-13 14:18pm    
Not true - you don't need curly brackets to delimit a block that consists of a single statement (but they are a good idea). His code indented shows what it happening without the brackets:
using (...)
using (...)
{
...
}

Try removing the "c:/" from your resource name.
 
Share this answer
 
Comments
Rezame 28-Aug-13 14:13pm    
Removed that.
Used only resource name, but same error.
tried add namespace name to that, same error again.
OriginalGriff 28-Aug-13 14:15pm    
Where is your code, and where is the resource? Are they in the same DLL/EXE?
Rezame 28-Aug-13 14:19pm    
This is part of console project.
First resource was in C drive, but now copied that in Project folder.
Copied in all folders of project.
Rezame 28-Aug-13 14:20pm    
project is Console.
First resource was in C drive, but next copied that in all project folders.
OriginalGriff 28-Aug-13 14:26pm    
So you just added the file to the project file? You didn't try to add it to the resources at all?
If you don't explicitly add a file to your resources, then it isn't available as a resource, it's just a file added to the project that needs no further attention from the compiler.
See here:
http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.90).aspx
I suspect you want to add your file as an 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900