Grabbing current user.config path (Visual Basic) - vb.net

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.

Related

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)

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.

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.

how to read the file which is in application itself and how to add in setup?

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).

Renaming a file in %appdata% in VB.net

So I need somone to tell me how to fix this code. I'm trying to rename a file which is in C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar to minecraft.jar.
The code I am using is:
My.Computer.FileSystem.RenameFile("C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar", "minecraft.jar")
Can someone fix this?
%appdata% not not a valid path, rather it denotes a special folder that you can get by using Environment.GetFolderPath, once a get the %appdata% path, you can easily rename file.
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim file_to_rename = Path.Combine(folder, ".minecraft\bin\XenonUpdate.jar")
My.Computer.FileSystem.RenameFile(file_to_rename, "minecraft.jar")
File handling functions do not deal with environment variable expansion, %appdata%. You need to do this yourself.
My VB.Net is non-existent, but I think it would look like
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim from = path + "\.minecraft..."
Dim to = path + "\.minecraft..."
My.Computer.FileSystem.RenameFile(from, to)
Also, see C# getting the path of %AppData%