使用 VB.NET 访问 %appdata%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6634054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:42:38  来源:igfitidea点击:

Accessing %appdata% with VB.NET

.netwindowsvb.netspecial-foldersappdata

提问by Kuzon

How can you access files in %appdata% through VB.NET?

如何通过 VB.NET 访问 %appdata% 中的文件?

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

例如,C:\Users\Kuzon\AppData\Roaming\program。我将如何访问该文件,但在另一台 Windows 7 机器上?另外,您将如何在 Windows XP 上执行此操作?我相信是的%Application Data%

回答by Cody Gray

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

在编写 .NET 代码时,建议您使用专门为此目的设计的函数,而不是依赖于%appdata%.

You're looking for the Environment.GetFolderPathmethod, which returns the path to the special folder that you specify from the Environment.SpecialFolderenumeration.

您正在寻找Environment.GetFolderPath方法,该方法返回您从Environment.SpecialFolder枚举中指定的特殊文件夹的路径。

The Application Data folder is represented by the Environment.SpecialFolder.ApplicationDatavalue. This is, as you requested, the roamingapplication 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.LocalApplicationDatavalue.

Application Data 文件夹由该Environment.SpecialFolder.ApplicationData值表示。这是您请求的漫游应用程序数据文件夹。如果您不需要保存的数据在多台机器上漫游,并且希望它只保留在本地,则应使用该Environment.SpecialFolder.LocalApplicationData值。

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.

是的,这在 C# 中的工作原理与 VB.NET 相同。

回答by gotorg

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

在 WinForms 中使用 VB.NET 时,这是另一种选择:

System.Windows.Forms.Application.UserAppDataPath

回答by HighTechProgramming15

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