CBreeze
CBreeze

Reputation: 2983

Embedding Text File in Resources C#

I'm attempting to do two things. I want to embed a text file into my project so that I can utilise it and modify it, but at the same time I don't want to have to package it when I send the project out to users (I.E included in the exe file).

I've had a look around and there's been multiple questions already but I just cant seem to get any to work. Here's the steps I've taken so far;

Added the text file to my "Resources Folder"

enter image description here

Build action to "Content" and output directory to "Do not copy"

enter image description here

I then try to access the file in my code;

if (File.Exists(Properties.Resources.company_map_template))
{
    MessageBox.Show("Test");
    var objReader = new StreamReader(Properties.Resources.company_map_template);
    string line = "";
    line = objReader.ReadToEnd();
    objReader.Close();
    line = line.Replace("[latlong]", latitude + ", " + longitude);
    mapWebBrowser.NavigateToString(line);
}

The MessageBox never appears which to me means that it cannot find the file and somewhere somehow I've done something wrong. How can I add the file into my project so I don't need to distribute with an exe whilst being able to access it in code?

Upvotes: 1

Views: 1640

Answers (1)

WPFGermany
WPFGermany

Reputation: 1647

I would use the following:

BuildAction to None (not needed)

and add your file to Resources.resx under files (using DragAndDrop from SolutionExplorer to opened Resources.resx)

Access to your Text:

using YOURNAMESPACE.Configuration.Properties;

string fileContent = Resources.company_map_template;

Then you're done. You don't need to access through StreamReader

Upvotes: 1

Related Questions