使用VB.NET访问%appdata% [英] Accessing %appdata% with VB.NET

查看:228
本文介绍了使用VB.NET访问%appdata%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如, C:\Users\Kuzon\ \AppData\漫游\程序。在另一台Windows 7计算机上,我将如何访问该文件?另外,您将如何在Windows XP上执行此操作?我相信它是%Application Data%

解决方案

重新编写.NET代码时,建议您使用为此目的显式设计的函数,而不要依赖环境变量,例如%appdata%



您正在寻找 Environment.GetFolderPath 方法,该方法将路径返回到您从 Environment.SpecialFolder 枚举



Application Data文件夹由 Environment.SpecialFolder.ApplicationData 值。根据您的要求,这是 roaming 应用程序数据文件夹。如果不需要保存保存的数据以在多台计算机上漫游,并且希望它们仅保留在本地,则应使用 Environment.SpecialFolder.LocalApplicationData 值。 / p>

完整示例代码:

 导入System.Environment 

类示例
Public Shared Sub Main()
'获取Application Data文件夹的路径
Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)

'显示路径
Console.WriteLine( App Data Folder Path:& appData)
结束子
结束类

是的,这在C#中与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 XP? 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.

这篇关于使用VB.NET访问%appdata%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆