如何确定Windows的“Download文件夹”路径? [英] How do I determine the Windows 'Download folder' path?
问题描述
在我的机器,它在这里:
On my machine, it's here:
string downloadsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Downloads");
但同事的机器,这个文件夹犯规存在,而且他的下载文件夹是在他的我的文档文件夹中。 <击>我们在Windows 7上都击> *
* 修改:其实,事实证明,他没有运行。他自己的机器,但在Windows Server 2003机器上的应用程序
*Edit: in fact, it turns out he was not running the app on his own machine but a Windows Server 2003 machine.
推荐答案
Windows不定义的CSIDL ,在下载文件夹,它通过的 Environment.SpecialFolder
枚举。
Windows does not define a CSIDL for the Downloads folder and it is not available through the Environment.SpecialFolder
enumeration.
然而,新的Vista 已知文件夹 API确实与 FOLDERID_Downloads的ID定义它
。可能获得的实际值的最简单方法是P /调用 SHGetKnownFolderPath
。
However, the new Vista Known Folder API does define it with the ID of FOLDERID_Downloads
. Probably the easiest way to obtain the actual value is to 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);
}
注意,P /上pinvoke.net则启动定是不正确,因为它失败使用Unicode字符集。此外,我已采取了这个API返回的内存由COM分配器分配的事实。在P的默认编组/调用上面是释放与 CoTaskMemFree
返回的内存,这是完美的满足我们的需要。
Note that the P/invoke given on pinvoke.net is incorrect since it fails to use Unicode character set. Also I have taken advantage of the fact that this API returns memory allocated by the COM allocator. The default marshalling of the P/invoke above is to free the returned memory with CoTaskMemFree
which is perfect for our needs.
请注意,这是一个Vista和API起来,不要试图把它称为对XP / 2003或更低。
Be careful that this is a Vista and up API and do not attempt to call it on XP/2003 or lower.
这篇关于如何确定Windows的“Download文件夹”路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!