Accessing files within project?

mattyd831

New member
Joined
Apr 16, 2008
Messages
1
Programming Experience
3-5
How am I able to access a file that I have added to my project from code. For instance, if I take a .sql script and add it into my project. How am I able to reference that file or point to it in code?

I obviously can use the path "C:\Temp\blah.sql", but how do I get to my file within the project???

Thanks
 
First you need to set the file as an embedded resource.

Next you need to use a little reflection to get it. The following will get a text file.

VB.NET:
Dim executing_assembly As System.Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
            Dim my_namespace As String = executing_assembly.GetName().Name.ToString()
            Dim text_stream As Stream
            text_stream = executing_assembly.GetManifestResourceStream(my_namespace & ".FileName.txt")

You can then use a stream reader to read line by line the text file.

VB.NET:
Dim myReader As New StreamReader(text_stream)
Dim line As String = ""
            While myReader.Peek <> -1
                   line = myReader.ReadLine()
                   'Do stuff
             End While
 
Back
Top