Trying to read a resource TXT file using GetManifestResourceStream
-
Tuesday, October 14, 2008 9:07 PM
Hey all,
I am getting the following error when calling the PrintHelp function below:
System.ArgumentNullException was unhandled
Message="Value cannot be null.\r\nParameter 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 ConvertCounterData.Program.PrintHelp() in C:\Documents and Settings\...\ConvertCounterData\Program.cs:line 323
at ConvertCounterData.Program.Main(String[] args) in C:\Documents and Settings\...\ConvertCounterData\Program.cs:line 20
at System.AppDomain.nExecuteAssembly(Assembly 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)
at System.Threading.ThreadHelper.ThreadStart()public static void PrintHelp()
I checked the Resources tab and my text file is there.
{
Assembly.GetExecutingAssembly().GetManifestResourceNames();
Console.WriteLine(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Helpfile.txt")).ReadToEnd());
}
What am I missing here?
Thanks
JCDS
JCDS
All Replies
-
Tuesday, October 14, 2008 10:48 PM
Since you say that you added the file as a resource (under the Project, ProjectName Properties, Resource tab), there is a very easy way to get its contents.
(Assumes that you chose FileType of Text in the Properties box for that resource. Otherwise, you would get a byte[].)
string myText = Properties.Resources.HelpFile;
Note that resources that you add through that tab are not directly accessible as "manifest resources". (Behind the scenes, .NET bundles all of the resources that you add using that tab into a single manifest resource for your assembly.)
To add and use a "manifest" resource directly, you do not need to use the Resources tab.
1. Make sure that the file is part of your project in Solution Explorer (for example, via Add, Existing Item).
2. Select the file in Solution Explorer and set the Build Action to "Embedded Resource".
3. Obtain and access Stream as follows (note that the resource name is prefixed with the name of the assembly):
using (Stream strm = @Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication1.HelpFile.txt")) { // Use strm here. } - Marked As Answer by Maximusdm Wednesday, October 15, 2008 12:10 AM
-
Wednesday, October 15, 2008 12:11 AMIt worked using the second method
Thank you!
JCDS
JCDS

