Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Experience is something you don't get until just after the moment you needed it
Posted

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?

Experience is something you don't get until just after the moment you needed it
  • Administrators
Posted
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....

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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();

Experience is something you don't get until just after the moment you needed it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...