Accessing %appdata% with VB.NET

本秂侑毒 提交于 2019-12-03 05:03:29

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!