Building a .txt file into my project

travisowens

Well-known member
Joined
Feb 11, 2004
Messages
108
Location
Rochester, NY
In my app I want to have a revision.txt file to keep track of revision history. I dont want to keep the file out in the open so I want a way to compile the .txt into my project. Im 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?

Ive heard I need to use resources but I dont 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.
 
The example worked, so I wanted to impliment it into my C# project so I ported the code...
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 its 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...
Code:
System.ArgumentNullException: Value cannot be null.
Parameter name: stream
Im 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?
 
travisowens said:
The example worked, so I wanted to impliment it into my C# project so I ported the code...
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 its 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...
Code:
System.ArgumentNullException: Value cannot be null.
Parameter name: stream
Im 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 doesnt fix it reply and Ill look into it a bit more....
 
Oppps, I didnt realize I was suspose to insert my namespace into the resource. Well anyways it works fine and heres the full code in case any future C# guys want to embed a text file into their app.


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
      // NOTE: the format is the Applications RootNamespace.resourcename
      s = myAssem.GetManifestResourceStream("Blotter.versions.txt");
                        
      // since its 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();
 
Back
Top