Beispiel #1
0
        /// <summary>
        /// Changing folder configuration file
        /// </summary>
        /// <param name="pBackgroundWorker">Background worker</param>
        /// <param name="pDirectoryInfo">Directory info</param>
        /// <param name="pFolderStatus">Folder status</param>
        protected void ChangeFolderConfigurationFile(DirectoryInfo pDirectoryInfo, FolderStatus pFolderStatus)
        {
            if (pFolderStatus.IconPath.Length == 0)
            {
                return;
            }

            string lIconPropertyValue           = Icons.IconConfigurationFilePropertyValue(pFolderStatus.IconPath);
            string lConfigurationFilePath       = Path.Combine(pDirectoryInfo.FullName, FolderIconChanger.ConfigurationFileName);
            string lConfigurationFileBackupPath = Path.Combine(pDirectoryInfo.FullName, FolderIconChanger.ConfigurationFileBackupName);
            ConfigurationFileSerialiser lConfigurationFileSerialiser = new ConfigurationFileSerialiser();
            ConfigurationFile           lConfigurationFile           = null;
            bool lNewConfigurationFile  = false;
            bool lSaveConfigurationFile = false;

            if (File.Exists(lConfigurationFilePath))
            {
                lConfigurationFile = lConfigurationFileSerialiser.Read(lConfigurationFilePath);
            }
            else
            {
                lConfigurationFile    = new ConfigurationFile();
                lNewConfigurationFile = true;
            }

            lConfigurationFile.SetSection(FolderIconChanger.ConfigurationFileSecionName);

            if (lNewConfigurationFile)
            {
                lConfigurationFile.SetProperty(FolderIconChanger.ConfigurationFilePropertyName, lIconPropertyValue);
                lSaveConfigurationFile = true;
            }
            else
            {
                string lPropertyValue = lConfigurationFile.GetProperty(FolderIconChanger.ConfigurationFilePropertyName);
                if (lPropertyValue != lIconPropertyValue)
                {
                    lConfigurationFile.SetProperty(FolderIconChanger.ConfigurationFilePropertyName, lIconPropertyValue);
                    lSaveConfigurationFile = true;
                }
                else
                {
                    pFolderStatus.IconAlreadyOk = true;
                }
            }

            if (lSaveConfigurationFile)
            {
                FileAttributes lConfigurationFileAttributes = FileAttributes.Archive | FileAttributes.Hidden | FileAttributes.System;
                if (!lNewConfigurationFile)
                {
                    if (File.Exists(lConfigurationFileBackupPath))
                    {
                        File.Delete(lConfigurationFileBackupPath);
                    }
                    File.Copy(lConfigurationFilePath, lConfigurationFileBackupPath);
                    lConfigurationFileAttributes = File.GetAttributes(lConfigurationFilePath);
                    File.SetAttributes(lConfigurationFilePath, FileAttributes.Normal);
                }
                lConfigurationFileSerialiser.Write(lConfigurationFile, lConfigurationFilePath);
                File.SetAttributes(lConfigurationFilePath, lConfigurationFileAttributes);

                pFolderStatus.IconChanged = true;
            }

            FileAttributes lFolderAttributes    = pDirectoryInfo.Attributes;
            FileAttributes lFolderNewAttributes = lFolderAttributes | FileAttributes.ReadOnly;

            if (lFolderAttributes != lFolderNewAttributes)
            {
                File.SetAttributes(pDirectoryInfo.FullName, lFolderNewAttributes);
            }
        }