Friday, March 4, 2011

Access internal file VB.NET

I added a text file to a testapp's solution and I want to read said file. I don't remember how to do this, I know it has to do with reflections but I need a push in the right direction.

From stackoverflow
  • I was going to write some code, but instead found a better explanation here

    In your case, you'll need to use your application path instead of "C:\".

  • If you add the text file to a .ResX file, you can have all the benefits (dynamic updating, for instance) AND not need to worry about interacting with an actual file. VB will automatically create a class to access the file - suppose you have Resources.resx. You can access it with My.Resources.MyFile - it will return a string.

    Anders : even better, thanks a lot :D
  • Did you add the file as a resource? In that case you can access its (String) content by just using My.Resources.name_of_file. Otherwise, the easiest way to read a text file in VB is to use the following.

    Dim content = My.Computer.FileSystem.ReadAllText("filename")
    
  • Are you talking about embedding the text file as a resource in the solution - ie compiled in?

    For that you need an instance of an assembly class (referecning yours, Assembly has a static member called GetExecutingAssembly) From there you call the GetManifestResourceStream

0 comments:

Post a Comment