簡體   English   中英

如何保持/保護文件不被修改?

[英]How to keep/protect a file from being modified?

在我正在編寫的應用程序中,我試圖通過修改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); }

當我點擊按鈕時,當我第一次啟動谷歌瀏覽器時,默認主頁更改成功,但關閉它並再次打開后,我注意到prefrences文件將更改為修改前的狀態。 對於 Mozilla Firefox 和 Inte.net Explorer 一切正常,但對於 Google Chrome,它只工作一次,瀏覽器重啟后,文件恢復為修改前的狀態。

也許,可以讓我的文件保持運行,並每隔一段時間檢查是否有任何修改,但我知道這將是資源密集型的,因此我正在尋找讓我成功更改默認主頁的方法。

我如何通過保護文件不被 Google Chrome 再次修改來保留我對文件的修改,或者是否有另一種方法可以永久更改默認主頁?

您的 Google Chrome 很可能已設置為將您的偏好設置與您的 Google 帳戶同步。 手動編輯文件不會將首選項更改推送到您的 Google 帳戶。 因此,當它再次加載時,它會從您的帳戶而不是該文件中獲取更新的首選項。

你需要查看Preferences中的“synch”object,並嘗試將上次同步時間設置為超出當前時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM