1

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?

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • I think the problem is it isn't quite the same when you have an embedded file, so you can't check for it's existence using `File.Exists`. If you want to read an embedded file, [check this out](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file). I don't think you are going to be able to write to an embedded file though, as that would essentially be modifying the EXE file. Something like this should typically be handled using an APP DATA folder for your application – musefan Sep 28 '16 at 09:36
  • @musefan Thanks for your suggestions I'll take them on board. – CBreeze Sep 28 '16 at 09:49

1 Answers1

1

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

WPFGermany
  • 1,639
  • 2
  • 12
  • 30