如何从asp.net c#访问浏览器下载位置

时间:2015-01-03 03:57:53

标签: c# asp.net json google-chrome

祝你新年快乐2015年。

如何从asp.net代码访问浏览器默认下载位置路径?我的应用程序支持多种浏览器,包括IE / chrome / Opera / Safari等,具有不同的OS XP,WIN7,8和mac系统,并且每个浏览器都配置了默认的下载位置路径。

我看过这篇文章:Programmatically access the Google Chrome Home or Start page

这篇文章与chrome有关,如何使用其他浏览器如Opera和其他mac系统safari?

我也试过发布的代码来检索 default_directory ,但是没有用?我不知道如何解决?如果任何人有支持不同操作系统和浏览器的通用解决方案请告诉我,非常感谢您的帮助。感谢

示例json(来自首选项文件)

{    " autofill":{       " negative_upload_rate":1.0,       " positive_upload_rate":1.0    },

"浏览器":{       " clear_lso_data_enabled":是的,       " last_known_google_url":" https://www.google.com/",       " last_prompted_google_url":" https://www.google.com/",       " pepper_flash_settings_enabled":是的,       " window_placement":{          "底部":728,          "离开":316,          "最大化":真实,          "对":1366,          " top":20,          " work_area_bottom":728,          " work_area_left":0,          " work_area_right":1366,          " work_area_top":0       }    },

" countryid_at_install":21843,    " default_apps_install_state":2,    " default_search_provider":{       " synced_guid":" 23210968-8CD1-4470-8885-E842444794F8"    },

"下载":{       " default_directory":" C:\ Users \ PC-RAJA \ Downloads",       " directory_upgrade":true    } }

c#c​​ode:



class Program
    {
        [DataContract]
        public class Mdata
        {
            [DataMember(Name = "default_directory")]
            public String default_directory { get; private set; }

            public Mdata() { }
            public Mdata(String data)
            {
                default_directory = data;
            }
        }



        public static Mdata FindData(String json)
        {

            Mdata deserializedData = new Mdata();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedData.GetType());
            deserializedData = ser.ReadObject(ms) as Mdata;
            ms.Close();
            return deserializedData;
        }

        static void Main(string[] args)
        {
            const int LikeWin7 = 6;
            OperatingSystem osInfo = Environment.OSVersion;
            DirectoryInfo strDirectory;
            String path = null, file = null, data;

            if (osInfo.Platform.Equals(System.PlatformID.Win32NT))
                if (osInfo.Version.Major == LikeWin7)
                    path = Environment.GetEnvironmentVariable("LocalAppData") +
                        @"\Google\Chrome\User Data\Default";
            if (path == null || path.Length == 0)
                throw new ArgumentNullException("Fail. Bad OS.");
            if (!(strDirectory = new DirectoryInfo(path)).Exists)
                throw new DirectoryNotFoundException("Fail. The directory was not fund");
            if (!new FileInfo(file = Directory.GetFiles(strDirectory.FullName, "Preferences*")[0]).Exists)
                throw new FileNotFoundException("Fail. The file was not found.", file);



            Mdata info = FindData(data = System.IO.File.ReadAllText(file));
            Console.WriteLine(info.default_directory);

            Console.ReadLine();
        }
    }




0 个答案:

没有答案