中丢失的文件夹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)
Results:
![enter image description here](https://i.stack.imgur.com/xV2Rv.jpg)
![enter image description here](https://i.stack.imgur.com/66rid.png)
注意:返回的文件夹显然不需要存在。特别是使用默认版本时,返回的几个文件夹实际上并不存在于我的系统上。