Accessing %appdata% with VB.NET

北城余情 提交于 2019-12-03 15:29:41

问题


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


回答1:


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.




回答2:


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

System.Windows.Forms.Application.UserAppDataPath



回答3:


Function GetAppDataPath() As String
   Return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
End Function


来源:https://stackoverflow.com/questions/6634054/accessing-appdata-with-vb-net

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