[英]C# get location of knownfolder by ID
I want to get the location of a directory/folder by its ID. 我想通过其ID获取目录/文件夹的位置。
For example, the Downloads folder has the ID knownfolder:{374DE290-123F-4565-9164-39C4925E467B}
, when I enter it into the address bar of windows explorer, it redirects my to the downloads folder. 例如,下载文件夹具有ID
knownfolder:{374DE290-123F-4565-9164-39C4925E467B}
文件夹knownfolder:{374DE290-123F-4565-9164-39C4925E467B}
,当我将其输入到Windows资源管理器的地址栏中时,它会将我重定向到下载文件夹。
There is a list of these IDs and the corresponding folders here , so I could hardcode the IDs and look them up like this, but I wouldnt want to do this unless its the only way. 有这些ID和相应的文件夹列表在这里 ,所以我可以硬编码ID和找一找这样,但我不会想这样做,除非它的唯一途径。
Is there another way to properly get what I want? 还有另一种方法来正确得到我想要的东西吗?
Stolen from here . 从这里偷来了 。 Looking at this further, the only way to do it is using a WinAPI/PInvoke
进一步看,唯一的方法是使用WinAPI / PInvoke
public static class KnownFolderFinder
{
private static readonly Guid CommonDocumentsGuid = new Guid("ED4824AF-DCE4-45A8-81E2-FC7965083634");
[Flags]
public enum KnownFolderFlag : uint
{
None = 0x0,
CREATE = 0x8000,
DONT_VERFIY = 0x4000,
DONT_UNEXPAND= 0x2000,
NO_ALIAS = 0x1000,
INIT = 0x800,
DEFAULT_PATH = 0x400,
NOT_PARENT_RELATIVE = 0x200,
SIMPLE_IDLIST = 0x100,
ALIAS_ONLY = 0x80000000
}
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
public static string GetFolderFromKnownFolderGUID(Guid guid)
{
return pinvokePath(guid, KnownFolderFlag.DEFAULT_PATH);
}
public static void EnumerateKnownFolders()
{
KnownFolderFlag[] flags = new KnownFolderFlag[] {
KnownFolderFlag.None,
KnownFolderFlag.ALIAS_ONLY | KnownFolderFlag.DONT_VERFIY,
KnownFolderFlag.DEFAULT_PATH | KnownFolderFlag.NOT_PARENT_RELATIVE,
};
foreach (var flag in flags)
{
Console.WriteLine(string.Format("{0}; P/Invoke==>{1}", flag, pinvokePath(CommonDocumentsGuid, flag)));
}
Console.ReadLine();
}
private static string pinvokePath(Guid guid, KnownFolderFlag flags)
{
IntPtr pPath;
SHGetKnownFolderPath(guid, (uint)flags, IntPtr.Zero, out pPath); // public documents
string path = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
return path;
}
}
And you could then call something like this: 然后你可以调用这样的东西:
var folder = KnownFolderFinder.GetFolderFromKnownFolderGUID(new Guid("374DE290-123F-4565-9164-39C4925E467B");
I think you're looking for Environment.SpecialFolder (System Namespace): 我想你正在寻找Environment.SpecialFolder(系统命名空间):
https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx
// Sample for the Environment.GetFolderPath method
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.System));
}
}
/*
This example produces the following results:
GetFolderPath: C:\WINNT\System32
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.