Accessing %appdata% with VB.NET
Asked Answered
G

4

21

How can you access files in %appdata% through VB.NET?

For example, C:\Users\Kuzon\AppData\Roaming\program. How would I access that file, but on another Windows 7 machine? Also, how would you do it on Windows XP? I believe it is %Application Data%.

Gypsie answered 9/7, 2011 at 9:53 Comment(1)
Thanks for trying to query the system for the correct path instead of hard-coding it like a lesser developer would. +1Upsilon
U
46

When you're writing .NET code, it's recommended that you use the functions explicitly designed for this purpose, rather than relying on environment variables such as %appdata%.

You're looking for the Environment.GetFolderPath method, which returns the path to the special folder that you specify from the Environment.SpecialFolder enumeration.

The Application Data folder is represented by the Environment.SpecialFolder.ApplicationData value. This is, as you requested, the roaming application data folder. If you do not need the data you save to roam across multiple machines and would prefer that it stays local to only one, you should use the Environment.SpecialFolder.LocalApplicationData value.

Full sample code:

Imports System.Environment

Class Sample
    Public Shared Sub Main()
        ' Get the path to the Application Data folder
        Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)

        ' Display the path
        Console.WriteLine("App Data Folder Path: " & appData)
    End Sub
End Class

And yes, this works in C# the same as VB.NET.

Upsilon answered 9/7, 2011 at 10:18 Comment(0)
I
3

When using VB.NET with WinForms, this is another option:

System.Windows.Forms.Application.UserAppDataPath
Isotone answered 24/3, 2016 at 20:41 Comment(0)
H
2
Function GetAppDataPath() As String
   Return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
End Function
Healthy answered 21/5, 2016 at 6:2 Comment(0)
G
0

It's not only knowing where the application data are, but rather, allowing users to set which folder they want to be used as default. Some users aren't administrators, and can only use local or roaming, but you really don't know, so you have to use Try..Catch. Also, other users may need to use a network to access data, so their working folder is Roaming.

For any user, I allow them set their working directory, and also allow for a custom folder, which is usually for people with their own PC/laptop, who are their own administrator. Below are just the My.Settings commands.

I also create an OutputDirectory (folder) into which application results are saved. (they will have disk read & write privileges if they can access the parent working directory in use). If not, they have to get their IT to set their privileges.

    Dim mdfolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Company Name"
    If Directory.Exists(mdfolder) = False Then Directory.CreateDirectory(mdfolder)
    Dim expfolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Company Name\AppName"
    If Directory.Exists(expfolder) = False Then Directory.CreateDirectory(expfolder)
    My.Settings.MyDocumentsFolder = expfolder
    mdfolder = expfolder
    My.Settings.Save()

    Dim roamfolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\AppName"
    My.Settings.RoamingDataFolder = roamfolder
    My.Settings.Save()

    If My.Settings.DefaultDataFolderOption = 1 Then
        DefaultDataFolder = roamfolder
    End If
    If My.Settings.DefaultDataFolderOption = 2 Then
        DefaultDataFolder = mdfolder
    End If
    If My.Settings.DefaultDataFolderOption = 3 Then
        DefaultDataFolder = My.Settings.CustomDataFolder
    End If
    If DefaultDataFolder = "" Then
        DefaultDataFolder = mdfolder
    End If
    If OutputDirectory = "" Then OutputDirectory = DefaultDataFolder & "\Output"
Good answered 7/8, 2021 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.