Accessing %appdata% with VB.NET

后端 未结 3 2074
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 05:49

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

3条回答
  •  臣服心动
    2021-02-07 06:39

    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.

提交回复
热议问题