使用C#更改浏览器下载文件夹 [英] Change Browser download folder using C#
问题描述
有没有一种方法可以使用c#更改默认Web浏览器的下载文件夹.
Is there a way I could change the download folder of the default web browser using c#.
推荐答案
与他人的评论并驾齐驱,只有在您拥有正确权限的情况下,您才可以在桌面应用程序中进行操作.
Concurring with other's comments, you can only do it in a desktop app if you have the right permissions.
Here's some sample code to find out the default browser of the system (from this post):
private string getDefaultBrowser()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe")+4);
}
}
finally
{
if (key != null) key.Close();
}
return browser;
}
但是,事情从这里变得棘手.不同的浏览器使用不同的方法来保存默认位置.
However, things get tricky from here. Different browsers have different ways of saving the default location.
例如
- IE可以将其存储在注册表中(通常在
HKEY_CURRENT_USER\ Software\ Microsoft\ Internet Explorer
下) - FF将其存储在
Profile
文件夹中的 prefs.js 中(签出此发布以通过代码访问它) - 不确定Chrome和Safari
- IE may store it in registry (usually under
HKEY_CURRENT_USER\ Software\ Microsoft\ Internet Explorer
) - FF stores it in prefs.js in
Profile
folder (checkout this post to get to it via code) - Not sure about Chrome and Safari
但是你明白了.
不确定最终目标是什么,但是从UX的角度来看,我认为最好的办法是 询问 用户指定下载目录(在换句话说,您会在应用程序中显示默认下载位置的设置).
Not sure what your end goal is, but from a UX standpoint, I think the best thing to do would be to ask user to specify the Download directory (in other words, you expose a Setting in your App for the default download location).
这篇关于使用C#更改浏览器下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!