Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a text file to be a part of my build?

Tags:

vb.net

build

I wrote a program that reads from text files and can create them to load and save data. I have a few files that are the "default" data that are loaded as soon as the program start. The files are loaded by a static reference. My code runs fine before I publish it, but obviously when I publish it, the static references no longer work. I don't know how to add the default data to the build as distinct text files so that I can still reference it after the build.

I imagine being able to build the program and have some sort of folder that accompanies the executable with the default data files in them that I can easily reference, but I don't know how to do that (or if there is a better way).

Below is the start of the code I use to read from the file. Currently, the default data's file name is passed statically into the sub and is used to identify the file to read from, so I'd like to have a published file that I can do the same thing with.

Try
    Dim sr As New IO.StreamReader(FileName)
    Dim strLine As String = ""

    Do Until sr.EndOfStream
        strLine = sr.ReadLine
        'Code that interprets the data in the file

Note: I've tried adding the files as "Resources" but I can't seem to reference the file as a text file; I can only retrieve the massive wall of text contained within the document which won't work with the above code (unless of course I'm missing something).

If you could clarify:

  1. How do I add a file to a build so that I can still access it collectively by a file name?
  2. How will my code reference the files (e.g. by "My.Resources.filename"?) in the final build?
like image 203
Paul Avatar asked May 22 '13 21:05

Paul


People also ask

How do I set up a text file?

Another way to create a text file is to right-click an empty area on the desktop, and in the pop-up menu, select New, and then select Text Document. Creating a text file this way opens your default text editor with a blank text file on your desktop.

How do you add a text file to a Java project?

Put the file in the folder from where you run your Java application (your current/working folder). If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder.

What makes a text file a text file?

A text file is a computer file that only contains text and has no special formatting such as bold text, italic text, images, etc. With Microsoft Windows computers text files are identified with the . txt file extension, as shown in the example picture.


1 Answers

You can add the file to the build as either a content file or an embedded resource.

For a content file, set the Build Action of the file to 'content', and Copy to Output Directory to 'Copy Always' in the file properties. You can then access the file like this:

FileName = Application.StartupPath() = + FileName
Dim sr As New IO.StreamReader(FileName)
...

To embed the file as a resource you have to set the Build Action of the file to 'Embedded Resource' and Copy to Output Directory to false. This Microsoft support page has a walkthough about accessing embedded resources. The code would be something like this:

Dim sr As StreamReader
Dim thisAssembly As Assembly
thisAssembly = Assembly.GetExecutingAssembly()
sr = New StreamReader(thisAssembly.GetManifestResourceStream("NameSpace." + FileName))

Dim strLine As String = ""
Do Until sr.EndOfStream
    strLine = sr.ReadLine
    'Code that interprets the data in the file
    ...

Replace NameSpace with the namespace of your application (Project Properties -> Application -> root namespace)

You also have to add Imports System.Reflection at the top of your code file.

Using an embedded resource has the advantage of less files to manage, and you don't have to keep track of paths.

like image 117
Paul Anderson Avatar answered Nov 09 '22 16:11

Paul Anderson