Windows没有为“下载”文件夹定义 CSIDL ,并且通过 Environment.SpecialFolder
枚举无法使用它。
但是,新的Vista
知名文件夹 API确实使用ID定义它FOLDERID_Downloads
。获取实际值的最简单方法可能是P / invoke SHGetKnownFolderPath
。 public static class KnownFolder
{
public static readonly Guid Downloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");
}
[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out string pszPath);
static void Main(string[] args)
{
string downloads;
SHGetKnownFolderPath(KnownFolder.Downloads, 0, IntPtr.Zero, out downloads);
Console.WriteLine(downloads);
}
请注意,pinvoke.net上给出的P / invoke不正确,因为它无法使用Unicode字符集。此外,我还利用了这个API返回COM分配器分配的内存这一事实。上面P / invoke的默认编组是释放返回的内存,
CoTaskMemFree
这对我们的需求是完美的。
请注意,这是一个Vista和更高版本的API,不要试图在XP / 2003或更低版本上调用它。