24

I need to read a file from my resources and add it to a list. my code:

private void Form1_Load(object sender, EventArgs e)
{
    using (StreamReader r = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.myText.txt")))
    {
        //The Only Options Here Are BaseStream & CurrentEncoding
    }
}

Ive searched for this and only have gotten answers like "Assembly.GetExecutingAssembly...." but my program doesnt have the option of Assembly.?

Dobz
  • 1,213
  • 1
  • 14
  • 36

4 Answers4

41

Try something like this :

string resource_data = Properties.Resources.test;
List<string> words = resource_data.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList();

Where

enter image description here

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • I did this a long time ago, and it worked great. I copied the code to a new project, and now I get an error, `Name Properties doesn't exist`. Do I need any libraries or packages for this solution> – Joe Dec 22 '22 at 17:13
11

You need to include using System.Reflection; in your header in order to get access to Assembly. This is only for when you mark a file as "Embedded Resource" in VS.

var filename = "MyFile.txt"
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNameSpace." + filename));

As long as you include 'using System.Reflection;' you can access Assembly like this:

Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace." + filename);

Or if you don't need to vary filename just use:

Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.MyFile.txt");

The full code should look like this:

using(var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.m‌​yText.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // Do some stuff here with your textfile
    }
}
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
  • 1
    `StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.myText.txt")) { `The Only Options Here are BaseStream & CurrentEncoding` };` – Dobz Mar 30 '13 at 19:42
  • What version of .net are you using? And you're saying reader only has `reader.BaseStream` and `reader.CurrentEncoding`? – Pete Garafano Mar 30 '13 at 19:52
  • Version 4? Yup, that's all that comes up? – Dobz Mar 30 '13 at 20:01
  • I can't answer why you are missing all the methods. You seem to only have properties available to you. Have you tried compiling it even though the IDE is unhappy with your calling of those method? You can also try cleaning your project and restarting VS. – Pete Garafano Mar 30 '13 at 20:06
3

Just to follow on this, AppDeveloper solution is the way to go.

string resource_data = Properties.Resources.test;
string [] words = resource_data.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
foreach(string lines in words){
.....
}
bummi
  • 27,123
  • 14
  • 62
  • 101
thaFaxGuy
  • 69
  • 2
1
        [TestCategory("THISISATEST")] 
        public void TestResourcesReacheability() 
        { 
            byte[] x = NAMESPACE.Properties.Resources.ExcelTestFile; 
            string fileTempLocation = Path.GetTempPath() + "temp.xls"; 
            System.IO.File.WriteAllBytes(fileTempLocation, x);   

            File.Copy(fileTempLocation, "D:\\new.xls"); 
        }

You get the resouce file as a byte array, so you can use the WriteAllBytes to create a new file. If you don't know where can you write the file (cause of permissions and access) you can use the Path.GetTempPath() to use the PC temporary folder to write the new file and then you can copy or work from there.

Danny Fallas
  • 628
  • 1
  • 5
  • 11