Accessing %appdata% with VB.NET
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.EnvironmentClass 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 SubEnd Class
And yes, this works in C# the same as VB.NET.
When using VB.NET with WinForms, this is another option:
System.Windows.Forms.Application.UserAppDataPath
Function GetAppDataPath() As String Return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)End Function