VB.NET | 获取当前用户配置文件文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18230411/
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-09 17:05:00  来源:igfitidea点击:

VB.NET | Get current user profile folder

vb.netusername

提问by user2681588

How can I use 'path'to go to the user's current profile?

如何使用'path'转到用户的当前个人资料?

For example, I have this code:

例如,我有这个代码:

        Dim fso, fldr
    fso = CreateObject("Scripting.FilesystemObject")

    fldr = fso.GetFolder("C:\Documents and Settings\%UserProfile%\Local Settings\TEST")

    'delete subfolders
    For Each subf In fldr.SubFolders

        subf.Delete(True)

    Next

    'delete subfiles
    For Each fsofile In fldr.Files

        fsofile.Delete(True)

    Next

I've tried this way and the path is unknown.

我试过这种方式,路径未知。

How can I make C:\Documents and Settings\???\Local Settings\TESTto go to the current user's folder?

如何C:\Documents and Settings\???\Local Settings\TEST转到当前用户的文件夹?

回答by Lectere

Use the 'userprofile' environment variable...

使用“userprofile”环境变量...

MsgBox(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))

回答by Ruturaj Patki

On my Windows 8.1, I cannot access Local Settings folder. It's right protected. As far as getting the right folder path is concerned, I think the answer is already posted above. Just append your custom folder path to the UserProfileFolder path returned by Environmentof DotNet.

在我的 Windows 8.1 上,我无法访问本地设置文件夹。它受到了正确的保护。至于获得正确的文件夹路径,我认为答案已经在上面发布了。只需将您的自定义文件夹路径附加到DotNetUserProfile返回的文件夹路径Environment

Something like:

就像是:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Local Settings\TEST"

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Local Settings\TEST"

回答by monami

Get the Local AppData folder:

获取本地 AppData 文件夹:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

And then concatenate it with your "TEST" folder using Path.Combinemethod.
See SpecialFoldersand Combinemsdn pages.

然后使用Path.Combine方法将它与您的“TEST”文件夹连接起来。
请参阅特殊文件夹组合msdn 页面。

回答by user4565320

This worked for me, using VB6.0 Sp6

这对我有用,使用 VB6.0 Sp6

    Dim myDocuPath As String
    myDocuPath = Environ$("USERPROFILE") & "\My Documents"