如何保护/保护文件不被修改?

时间:2012-04-06 20:03:18

标签: c# .net

在我写的应用程序中,我正在尝试通过修改preferences文件来更改Google Chrome的默认主页

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

    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); }

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

也许,可以保持我的文件运行,并检查每个时间间隔的任何修改,但我知道这将是资源密集型,因此我正在搜索某些让我更改默认主页的成功方式。

如何保护我对文件的修改,防止其被Google Chrome再次修改,或者是否有其他方法可以永久更改默认主页?

1 个答案:

答案 0 :(得分:4)

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

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

相关问题