Accessing %appdata% with VB.NET
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 X开发者_运维百科P? I believe it is %Application Data%
.
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.
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
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"
精彩评论