【问题标题】:How to keep/protect a file from being modified?如何保持/保护文件不被修改?
【发布时间】:2012-04-06 20:03:17
【问题描述】:

在我正在编写的应用程序中,我试图通过修改preferences 文件来更改谷歌浏览器的默认主页

为了实现这一点,我使用以下代码片段

    private void button1_Click(object sender, EventArgs e)
    {
        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.homepage);
       // Console.WriteLine(info.isNewTab);
        MessageBox.Show(info.homepage);


        // replace text now
        String strFile = File.ReadAllText(file);

        strFile = strFile.Replace(info.homepage, "www.monde-presse.com");

        File.WriteAllText(file, strFile); }

当我点击按钮时,第一次启动谷歌浏览器时默认主页更改成功,但在关闭它并再次打开后,我注意到prefrences文件将更改为以前的样子修改它。对于 Mozilla Firefox 和 Internet Explorer,一切正常,但对于 Google Chrome,它只能工作一次,在浏览器重新启动后,文件会变成修改之前的状态。

也许,可以让我的文件保持运行,并在每个时间间隔检查是否有任何修改,但我知道这会占用大量资源,因此我正在寻找某种方法,让我可以成功更改默认主页方式。

我怎样才能通过保护文件不被谷歌浏览器再次修改来保存我对文件的修改,或者也许还有另一种方法可以永久更改默认主页?

【问题讨论】:

  • 谷歌浏览器显然在使用某种机制来防止人们在不受欢迎的庄园中更改内部设置。问题不应该是“我如何打败 Google Chrome 的防御/安全”,而是“我如何supposed 以编程方式永久更改 Google Chrome 中的设置。这是一个 Google Chrome 问题,不是 C#/文件系统类型的问题。我当然不知道如何应该修改 Google Chrome。
  • 如果您确实成功地“保护”了首选项,Chrome 用户将永远无法再次更改设置...

标签: c# .net


【解决方案1】:

您的 Google Chrome 很可能设置为将您的偏好设置与您的 Google 帐户同步。手动编辑文件不会将首选项更改推送到您的 Google 帐户。因此,当它再次加载时,它会从您的帐户而不是该文件中获取更新后的首选项。

您需要查看 Preferences 中的“同步”对象,并尝试将上次同步时间设置为超出当前时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2011-12-18
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    相关资源
    最近更新 更多