使用C#更改浏览器下载文件夹 [英] Change Browser download folder using C#

查看:362
本文介绍了使用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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆