Beispiel #1
0
        public static DirTable LoadDirTable(string filename)
        {
            string jsonString = "";

            try {
                jsonString = File.ReadAllText(filename);
            } catch (Exception) {
                Console.WriteLine("Dir table not found\nCreate new dir table");
            }
            try {
                DirTable dirTable = JsonConvert.DeserializeObject <DirTable>(jsonString);
                if (dirTable != null)
                {
                    return(dirTable);
                }
                else
                {
                    return(new DirTable());
                }
            } catch (JsonReaderException e) {
                Console.Error.WriteLine("Cannot parse config file");
                Console.Error.WriteLine(e);
                Environment.Exit(-1);
                return(null);
            }
        }
Beispiel #2
0
        public static void SaveDirTable(DirTable dirTable, string path)
        {
            string jsonString = JsonConvert.SerializeObject(dirTable, Formatting.Indented);

            try {
                File.WriteAllText(path, jsonString);
            }
            catch (Exception e) {
                Console.Error.WriteLine(e);
            }
        }
Beispiel #3
0
        public DirProt(bool backup)
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDir + "log.config"));
            Logger.Info("Loading config");
            Config        config = ConfigManager.LoadConfig(AppDir + "config.json");
            List <string> users  = new List <string>();

            foreach (string user in config.ProtectedTaskbar)
            {
                try {
                    NTAccount          account            = new NTAccount(user);
                    SecurityIdentifier securityIdentifier =
                        (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
                    string sid     = securityIdentifier.ToString();
                    string homeDir = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" + sid).GetValue("ProfileImagePath").ToString();
                    config.ProtectedDir.Add(homeDir + @"\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar");
                    users.Add(sid);
                } catch (Exception e) {
                    Console.Error.WriteLine(e);
                }
            }
            DirTable dirTable =
                ConfigManager.LoadDirTable(AppDir + "data" + Path.DirectorySeparatorChar + "dirtable.json");
            List <DirPath> directorys = dirTable.Directorys;

            if (config.Enabled || backup)
            {
                foreach (string iPath in config.ProtectedDir)
                {
                    Logger.Info("Checking \"" + iPath + "\" backup...");
                    string  path       = iPath.ToLower();
                    bool    isBackuped = false;
                    DirPath backupDir  = null;
                    foreach (DirPath directory in directorys)
                    {
                        if (path.Equals(directory.path))
                        {
                            isBackuped = true;
                            backupDir  = directory;
                            break;
                        }
                    }
                    if (isBackuped)
                    {
                        Logger.Info("Start restore backup");
                        DirectoryInfo directoryInfo = new DirectoryInfo(path);
                        if (directoryInfo.Exists)
                        {
                            foreach (FileSystemInfo childInfo in directoryInfo.GetFileSystemInfos())
                            {
                                try {
                                    DeleteReadOnly(childInfo);
                                } catch (Exception e) {
                                    Console.WriteLine(e);
                                }
                            }
                        }
                        CopyDirectory(AppDir + Path.DirectorySeparatorChar + "data" + Path.DirectorySeparatorChar + backupDir.hash + Path.DirectorySeparatorChar + backupDir.index, path);
                        Logger.Info("Successfully restore");
                    }
                    else
                    {
                        Logger.Info("Start backup");
                        Backup(path, directorys);
                        Logger.Info("Successfully backup");
                    }
                }
                foreach (string user in users)
                {
                    Logger.Info("Checking " + user + " taskbar backup...");
                    bool        isBackuped = false;
                    RegistryDir reg        = null;
                    foreach (RegistryDir registryDir in dirTable.Registries)
                    {
                        if (registryDir.path.Equals(user + @"\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband"))
                        {
                            isBackuped = true;
                            reg        = registryDir;
                            break;
                        }
                    }
                    if (isBackuped)
                    {
                        Logger.Info("Start restore taskbar registry...");
                        RegistryKey taskband = Registry.Users.OpenSubKey(reg.path, true);
                        while (taskband == null)
                        {
                            Thread.Sleep(5000);
                            taskband = Registry.Users.OpenSubKey(reg.path, true);
                        }
                        foreach (string valueName in taskband.GetValueNames())
                        {
                            taskband.DeleteValue(valueName);
                        }
                        foreach (RegistryData registryData in reg.RegistryData)
                        {
                            if (registryData.type.Equals(RegistryValueKind.Binary))
                            {
                                registryData.value = Convert.FromBase64String((string)registryData.value);
                            }
                            taskband.SetValue(registryData.name, registryData.value, registryData.type);
                        }
                        Logger.Info("Successfully restore taskbar");
                    }
                    else
                    {
                        Logger.Info("Start backup taskbar registry...");
                        reg      = new RegistryDir();
                        reg.path = user + @"\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband";
                        RegistryKey taskband = Registry.Users.OpenSubKey(reg.path);
                        while (taskband == null)
                        {
                            Thread.Sleep(5000);
                            taskband = Registry.Users.OpenSubKey(reg.path);
                        }
                        foreach (string valueName in taskband.GetValueNames())
                        {
                            RegistryData regData = new RegistryData();
                            regData.name  = valueName;
                            regData.type  = taskband.GetValueKind(valueName);
                            regData.value = taskband.GetValue(valueName);
                            reg.RegistryData.Add(regData);
                        }
                        dirTable.Registries.Add(reg);
                        Logger.Info("Successfully backup taskbar");
                    }
                }
                ConfigManager.SaveDirTable(dirTable, AppDir + Path.DirectorySeparatorChar + "data" + Path.DirectorySeparatorChar + "dirtable.json");
                if (backup)
                {
                    MessageBox.Show("Successfully backuped.", "DirProt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                Logger.Info("DirProt is disabled.");
            }
            Environment.Exit(0);
        }