【问题标题】:How to reference the current Windows user's video folder path in VB.net如何在 VB.net 中引用当前 Windows 用户的视频文件夹路径
【发布时间】:2015-12-10 00:05:30
【问题描述】:

我正在寻找一种方法来引用当前用户在 VB.NET 中的“MyVideos”文件夹。

我的目标是使用这个引用来设置我的OpenFileDialog 对象的InitialDirectory 属性。事情是这样的:

OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments

SpecialDirectories 下,我找不到 MyVideos 的属性。我在SpecialDirectories 下的唯一属性是:

.Desktop
.MyDocuments
.MyMusic
.MyPictures
.Programfiles
.Programs
.Temp

我错过了什么吗?还有其他方法可以访问这些信息吗?

我能够获取用户的根文件夹,并将其与“视频”结合起来,如下所示:

Dim vidPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Videos")

但是,这假设用户没有在其位置属性中更改其My Videos 文件夹的位置。

我想想出一个方法来引用这个位置,以防用户更改了这个位置设置。

【问题讨论】:

  • @Steve & @Plutonix 当然,答案很有帮助,因为它表明我可以使用dim vidPath as string = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "My Videos"),但是;假设用户没有更改其My Videos 文件夹的属性以指向 Windows 默认以外的位置。
  • 你也忘记了本地化问题。在我的国家是视频

标签: vb.net windows environment-variables openfiledialog


【解决方案1】:

Environment.SpecialFolder 枚举中缺少的文件夹可通过 API 调用获得。对此有几个 C# 答案,主要是部分答案(获取特定文件夹)。所有(?)缺失的 VB 版本:

Public Partial Class NativeMethods

    <DllImport("shell32.dll")>
    Private Shared Function SHGetKnownFolderPath(<MarshalAs(UnmanagedType.LPStruct)> 
                                            rfid As Guid,
                                            dwFlags As UInt32,
                                            hToken As IntPtr,
                                            ByRef pszPath As IntPtr) As Int32
    End Function

   ' in order, below are:
    Public Enum ShellSpecialFolders
        Contacts
        Downloads
        Links
        Music
        Pictures
        SavedGames
        SavedSearches
        Videos
    End Enum

    Private Shared ShellFolderGuids As Guid() = {
                        Guid.Parse("{56784854-C6CB-462B-8169-88E350ACB882}"),
                        Guid.Parse("{374DE290-123F-4565-9164-39C4925E467B}"),
                        Guid.Parse("{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"),
                        Guid.Parse("{4BD8D571-6D19-48D3-BE97-422220080E43}"),
                        Guid.Parse("{33E28130-4E1E-4676-835A-98395C3BC3BB}"),
                        Guid.Parse("{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"),
                        Guid.Parse("{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"),
                        Guid.Parse("{18989B1D-99B5-455B-841C-AB7C74E4DDFC}")
                                         }

    Friend Shared Function GetSpecialFolder(folder As ShellSpecialFolders) As String
        Dim ret As Int32
        Dim fPath As IntPtr
        ' == "Dont Vertify" flag:
        Dim SHFlag As UInt32 = &H4000

        ret = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag,
                                   New IntPtr(0), fPath)

        If ret = 0 Then
            Return Marshal.PtrToStringUni(fPath)
        Else
            Return ""
        End If
    End Function

    ' Optional single purpose version
    Friend Shared Function GetSpecialVideoFolder() As String
        Return GetSpecialFolder(ShellSpecialFolders.Videos)
    End Function
'...
End Class

示例用法:

spath = NativeMethods.GetSpecialFolder(NativeMethods.ShellSpecialFolders.Videos)
Console.WriteLine("Videos are in: {0}", spath)

或者如果你想为它们编写包装器:

spath = NativeMethods.GetSpecialVideoFolder()

如果您想获得默认文件夹(而不是 C:\Users\USER NAME\...,您将获得 C:\Users\Default\...)将 IntPtr 参数更改为 -1:

ret = SHGetKnownFolderPath(ShellFolderGuids(folder), SHFlag,
                               New IntPtr(-1), fPath)

结果:

注意:返回的文件夹显然不需要存在。尤其是使用默认版本,返回的几个文件夹在我的系统上实际上并不存在。

【讨论】:

  • 这正是我所需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 2013-07-26
  • 2010-11-22
  • 2019-02-01
  • 2010-11-01
  • 1970-01-01
相关资源
最近更新 更多