how to read the file which is in application itself and how to add in setup? - vb.net

I have a class library which reads the XML file.
I am using VS 2012 and VB.NET language.
I am getting confused about how to read the file which is in folder of a application itself.
Right now I have given the path as
Dim reader As XmlTextReader = New XmlTextReader("C:\mailpara.xml")
but its hard-coded , but I want to make a folder in app. and want to read from that
folder itself.
I want to know how to read the file from the folder of a application.
How to read the file after installation on client's machine and how to add the file while making the set up ?

Use something like;
Dim directory as String = My.Application.Info.DirectoryPath
Dim reader As XmlTextReader = New XmlTextReader(directory & "\MyFolderName\mailpara.xml")

You can use Application.StartupPath property to retrieve the startup path of the application.
Dim reader As XmlTextReader = New XmlTextReader(Application.StartupPath & "mailpara.xml")
You might want to add a check to ensure that the path ends with a \ (I think it may or may not be present depending on whether the path is a root folder or not).

Related

Grabbing current user.config path (Visual Basic)

Is there a way of grabbing the location of the My.Settings user.config location? So for example I want to be able in VB to grab the path of the user.cofing file path to a string
The reason I ask is that I have an application where the user.config file is backed up and then restored, the issue is that with the my.settings folder structure it uses a unique hash with the folder name meaning that I cannot write into the code a static folder path, instead I need to be able to grab the location of the user.config OR be able to get the folder name of the application AppData.
Any ideas?
To put this in perspective, currently I'm using something like this:
Dim filePath As String
filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\My_App\My_App.exe_Url_<the_hash_that_changes_causing_issues>\1.0.0.0\user.config"
Because of the hash change this will not always work
Try executing this code:
Dim mainConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim userConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming)
Dim userLocalConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
Console.WriteLine(mainConfig.FilePath)
Console.WriteLine(userConfig.FilePath)
Console.WriteLine(userLocalConfig.FilePath)
You'll need to reference System.Configuration.dll and import System.Configuration.

File Path of Access Database

I'm working with vb.net 2008 as well. But I have a question.How to remove a file path like this C:\users\myDocu\debug\Dbase.accdb and I only want is the file name Dbase.accdb. Because I want to transfer my files in another computer but the problem is the file path. I always need to change the entire location in my codes to run without debug.
To get the filename without the path, you can use Path.GetFileName.
But if you want a painless way to find a place to store your database, consider putting it into the application data folder (AppData). You can get this folder with Environment.GetFolderPath and Environment.SpecialFolder.ApplicationData, using it like this:
Dim pathToDb = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Dbase.accdb")
if you want to use the file locally. If you want to share the file between different instances of your application in a network, put the path e.g. in a config file like App.Config.
Try this:
Dim FullFilePath As String
Dim FileName As String
FullFilePath = "C:\users\myDocu\debug\Dbase.accdb"
FileName = Mid(FullFilePath,InStrRev(FullFilePath,"\") + 1)

Get a VB 2008 file path relative to the published executable location

I have a Visual Basic Studio 2008 project that I need to deploy in 3 separate server environments. Each of those environments has a different filepath for file storage, but other than that the execution of the programs will be exactly the same in all environments.
In order to accomplish this, I'd like to instruct the program to look at a text file in the same folder as itself for the file storage file path for its environment; then i can just clone the same VB program 3 times and change the contents of the text files whenever the storage locations change.
Before publishing an executable file, I can store the text file in the bin->debug folder and use any of the VB relative path methods that I've come across (App.Path, System.IO.getcurrentdirectory, My.Application.info.directoryPath, etc) to access it with no problem. When I publish the project, however, these find the relative path of the program as buried deep within the installed user's appdata. I want to access the text file on the server where the user goes to run the executable.
So my question is: how can i get the filepath of the published executable location? I have searched for 4 hours and have been unsuccessful in finding an answer.
Relevant code:
dim fso as new scripting.filesystemobject
dim ts as scripting.textStream
ts = fso.opentextfile(My.Application.Info.DirectoryPath & "\HostFiles\rootDir.txt")
rootDir = ts.ReadAll
topDir = rootDir & 'rest of file storage location
Maybe you could try this if you are using VB.Net:
Application.StartupPath
Eg:
Dim strContent As String = IO.File.ReadAllText(Application.StartupPath & "\HostFiles\rootDir.txt")

How do I get a text file to be a part of my 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:
How do I add a file to a build so that I can still access it
collectively by a file name?
How will my code reference the files (e.g. by
"My.Resources.filename"?) in the final build?
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.

Using a .txt file Resource in VB

Right now i have a line of code, in vb, that calls a text file, like this:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("data5.txt")
data5.txt is a resource in my application, however the application doesn't run because it can't find data5.txt. I'm pretty sure there is another code for finding a .txt file in the resource that i'm overlooking, but i can't seem to figure it out. So does anyone know of a simple fix for this? or maybe another whole new line of code? Thanks in advance!
If you added the file as a resource in the Project + Properties, Resources tab, you'll get its content by using My.Resources:
Dim content As String = My.Resources.data5
Click the arrow on the Add Resource button and select Add Existing File, select your data5.txt file.
I'm assuming that the file is being compiled as an Embedded Resource.
Embedded Resources aren't files in the filesystem; that code will not work.
You need to call Assembly.GetManifestResourceStream, like this:
Dim fileText As String
Dim a As Assembly = GetType(SomeClass).Assembly
Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt"))
fileText = reader.ReadToEnd()
End Using
First, go to project resources (My Project --> Resources), and drag-and-drop your file, say "myfile.txt", from the file system to the resourses page.
Then:
Imports System.IO
...
Dim stream As New MemoryStream(My.Resources.myfile)
Dim reader As New StreamReader(stream)
Dim s As String = reader.ReadToEnd()