当前位置: 首页 > 编程语言 > C#

以编程方式访问谷歌浏览器主页或起始页分享

时间:2023-04-10 14:07:08 C#

以编程方式访问谷歌浏览器主页或起始页我想使用C#以编程方式访问它。默认位置是:WindowsXPGoogleChrome:C:DocumentsandSettingsLocalSettingsApplicationDataGoogleChromeUserDataDefaultChromium:C:DocumentsandSettingsLocalSettingsApplicationDataChromiumUserDataDefaultVista/7GoogleChrome:C:UsersAppDataLocalGoogleChromeUserDataDefaultChromium:C:UsersAppDataLocalChromiumUserDataDefaultGoogleChromeMacOSX。:~/Library/ApplicationSupport/Google/Chrome/DefaultChromium:~/Library/ApplicationSupport/Chromium/DefaultGoogleChromeforLinux:~/.config/google-chrome/DefaultChromium:~/.config/chromium/Defaultsource:GoogleChromium用户数据目录的默认位置。(链接)在我写这篇文章的时候,这是我能想到的最短和最可靠的例子(我完全忽略了用户可以使用不同位置然后默认的事实)。必须说,这有点棘手然后我想。在此示例中,我尝试使用默认位置目录,并查找存储“Home”的首选项文件。它以JSon格式存储,因此我将我感兴趣的数据反序列化并打印出来。赢7例:使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.IO;//引用->添加引用->"System.Runtime.Serialization"添加usingSystem.Runtime.Serialization;使用System.Runtime.Serialization.Json;命名空间测试{classProgram{[DataContract]publicclassMdata{[DataMember(Name="homepage")]publicStringhomepage{get;私有集;}[DataMember(Name="homepage_is_newtabpage")]publicBooleanisNewTab{get;私有集;}publicMdata(){}publicMdata(Stringdata){homepage=data;}}publicstaticMdataFindData(Stringjson){MdatadeserializedData=newMdata();MemoryStreamms=newMemoryStream(Encoding.UTF8.GetBytes(json));DataContractJsonSerializerser=newDataContractJsonSerializer(deserializedData.GetType());deserializedData=ser.ReadObject(ms)作为Mdata;关闭();返回反序列化数据;}staticvoidMain(string[]args){constintLikeWin7=6;操作系统temosInfo=Environment.OSVersion;目录信息strDirectory;字符串路径=空,文件=空,数据;如果(osInfo.Platform.Equals(System.PlatformID.Win32NT))如果(osInfo.Version.Major==LikeWin7)path=Environment.GetEnvironmentVariable("LocalAppData")+@"GoogleChromeUserDataDefault";if(path==null||path.Length==0)thrownewArgumentNullException("Fail.BadOS.");if(!(strDirectory=newDirectoryInfo(path)).Exists)thrownewDirectoryNotFoundException("Fail.Thedirectorywasnotfund");if(!newFileInfo(file=Directory.GetFiles(strDirectory.FullName,"Preferences*")[0]).Exists)thrownewFileNotFoundException("失败。找不到文件。",file);Mdata信息=FindData(数据=System.IO.File.ReadAllText(文件));Console.WriteLine(info.homepage);Console.WriteLine(info.isNewTab);我的示例输出:chrome://newtabTrueHope我至少获得了1票:P在Windows7(我猜是Vista)上默认安装它存储在文件中:%USERPROFILE%AppDataLocalGoogleUserDataDefaultPreferences在Windows2003上(在XP上):%USERPROFILE%LocalSettingsApplicationDataGoogleChromeUserDataDefaultPreferences要查找的属性名称是:homepage您需要的首选项文件的小子集。以上是C#学习教程:编程访问谷歌浏览器首页或“开始”页共享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——},"homepage":"http://www.google.com/","homepage_is_newtabpage":true,"pinned_tabs":[{本文摘自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: