travisowens Posted March 4, 2004 Posted March 4, 2004 In my app I want to have a revision.txt file to keep track of revision history. I don't want to keep the file out in the open so I want a way to compile the .txt into my project. I'm assuming this is already happening when I add the .txt file to my project but the question is, how can I (open) StreamReader this file? I've heard I need to use resources but I don't even know where such an option lies in Visual Studio 2003. If somebody can give me a quick mini tutorial on this, I would be thankfull. Quote Experience is something you don't get until just after the moment you needed it
Administrators PlausiblyDamp Posted March 4, 2004 Administrators Posted March 4, 2004 As similar things have been asked before I've just thrown together a quick sample and posted in the code library. http://www.xtremedotnettalk.com/showthread.php?s=&threadid=83574 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
travisowens Posted March 10, 2004 Author Posted March 10, 2004 The example worked, so I wanted to impliment it into my C# project so I ported the code... // get a reference to the running assembly Assembly myAssem; myAssem = Assembly.GetExecutingAssembly(); // Stream for the resource System.IO.Stream s; // Get the resource stream from ourselves s = myAssem.GetManifestResourceStream("EmbeddedResources.versions.txt"); // as it's a text file create a stream reader... System.IO.StreamReader sr = new System.IO.StreamReader(s); // ...and read txtVersionHistory.Text = sr.ReadToEnd(); // be well behaved now sr.Close(); s.Close(); It builds fine but errors on runtime with... System.ArgumentNullException: Value cannot be null. Parameter name: stream I'm guessing it cannot find the file as an embedded resource but I have no idea why. I compare my app to the tutorial project and they appear to be exactly the same. Any ideas? Quote Experience is something you don't get until just after the moment you needed it
Administrators PlausiblyDamp Posted March 10, 2004 Administrators Posted March 10, 2004 The example worked, so I wanted to impliment it into my C# project so I ported the code... // get a reference to the running assembly Assembly myAssem; myAssem = Assembly.GetExecutingAssembly(); // Stream for the resource System.IO.Stream s; // Get the resource stream from ourselves s = myAssem.GetManifestResourceStream("EmbeddedResources.versions.txt"); // as it's a text file create a stream reader... System.IO.StreamReader sr = new System.IO.StreamReader(s); // ...and read txtVersionHistory.Text = sr.ReadToEnd(); // be well behaved now sr.Close(); s.Close(); It builds fine but errors on runtime with... System.ArgumentNullException: Value cannot be null. Parameter name: stream I'm guessing it cannot find the file as an embedded resource but I have no idea why. I compare my app to the tutorial project and they appear to be exactly the same. Any ideas? What is the default namespace for your application (you can check by bringing up the project properties). You need to change 'EmbeddedResources' to the applications default namespace for it to work. If that doesn't fix it reply and I'll look into it a bit more.... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
travisowens Posted March 17, 2004 Author Posted March 17, 2004 Oppps, I didn't realize I was suspose to insert my namespace into the resource. Well anyways it works fine and here's the full code in case any future C# guys want to embed a text file into their app. // get a reference to the running assembly Assembly myAssem; myAssem = Assembly.GetExecutingAssembly(); // Stream for the resource System.IO.Stream s; // Get the resource stream from ourselves // NOTE: the format is the Applications RootNamespace.resourcename s = myAssem.GetManifestResourceStream("Blotter.versions.txt"); // since it's a text file create a stream reader... System.IO.StreamReader sr = new System.IO.StreamReader(s); // ...and read txtVersionHistory.Text = sr.ReadToEnd(); txtVersionHistory.SelectionLength = 0; // be well behaved now sr.Close(); s.Close(); Quote Experience is something you don't get until just after the moment you needed it
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.