/// <summary>
        /// Checks whether the payload XML of all the communities are valid or not. If any file or folder of a community is changed
        /// after its payload file is created, then the payload file will be considered as invalid and deleted.
        /// </summary>
        private void CheckPayloadValidity()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(this.CommunityLocation))
                {
                    DirectoryInfo directory = new DirectoryInfo(this.CommunityLocation);

                    if (directory.Exists)
                    {
                        // Get all the directories excluding hidden, system and reparse point directories.
                        List <DirectoryInfo> directories = directory.GetDirectories().Where(
                            e => !e.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReparsePoint)).ToList();

                        foreach (DirectoryInfo dirInfo in directories)
                        {
                            string payloadFilePath = string.Format(CultureInfo.CurrentCulture, "{0}\\{1}_payload.xml", dirInfo.FullName, dirInfo.Name);
                            if (CommunityFolderUpdated(File.GetLastWriteTimeUtc(payloadFilePath), dirInfo.FullName))
                            {
                                // Add to the to be deleted list. In case if the file cannot be deleted at the moment, later those files can be deleted.
                                if (!FilesToBeDeleted.Contains(payloadFilePath))
                                {
                                    FilesToBeDeleted.Add(payloadFilePath);
                                }

                                File.Delete(payloadFilePath);
                                FilesToBeDeleted.Remove(payloadFilePath);
                            }
                        }
                    }
                }
            }
            catch (SecurityException ex)
            {
                ErrorHandler.LogException(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                ErrorHandler.LogException(ex);
            }
            catch (ArgumentException ex)
            {
                ErrorHandler.LogException(ex);
            }
            catch (IOException ex)
            {
                ErrorHandler.LogException(ex);
            }
            catch (Exception ex)
            {
                ErrorHandler.LogException(ex);
            }
        }
        /// <summary>
        /// Handles this renamed or changed events of the folder or file. Makes sure that the payload XML of the community is deleted
        /// if any changes to the file or folder is made.
        /// </summary>
        /// <param name="eventContentPath">Content (file or folder) path to which the event is triggered for</param>
        private void HandleCommunityFolderEvent(string eventContentPath)
        {
            try
            {
                this.CommunityFolderWatcher.EnableRaisingEvents = false;

                if (!eventContentPath.EndsWith("_payload.xml", StringComparison.OrdinalIgnoreCase))
                {
                    string communityFolderPath = eventContentPath.Replace(this.CommunityLocation, string.Empty).TrimStartSlashes();

                    int directorySeparatorIndex = communityFolderPath.IndexOf(Path.DirectorySeparatorChar);
                    directorySeparatorIndex = directorySeparatorIndex == -1 ? communityFolderPath.Length : directorySeparatorIndex;

                    communityFolderPath = Path.Combine(this.CommunityLocation, communityFolderPath.Substring(0, directorySeparatorIndex));

                    DirectoryInfo communityFolder = new DirectoryInfo(communityFolderPath);
                    if (communityFolder.Exists)
                    {
                        FileInfo[] payloadFile = communityFolder.GetFiles("*_payload.xml");

                        foreach (FileInfo file in payloadFile)
                        {
                            // Add to the to be deleted list. In case if the file cannot be deleted at the moment, later those files can be deleted.
                            if (!FilesToBeDeleted.Contains(file.FullName))
                            {
                                FilesToBeDeleted.Add(file.FullName);
                            }

                            file.Delete();
                            FilesToBeDeleted.Remove(file.FullName);
                        }
                    }

                    // Delete if any payload files are pending to be deleted, because cache dependency could not delete last time.
                    for (int i = FilesToBeDeleted.Count - 1; i >= 0; i--)
                    {
                        string filePath = FilesToBeDeleted[i];
                        File.Delete(filePath);
                        FilesToBeDeleted.Remove(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.LogException(ex);
            }
            finally
            {
                this.CommunityFolderWatcher.EnableRaisingEvents = true;
            }
        }
Ejemplo n.º 3
0
 public void RegesterFileForCleanUp(string path)
 {
     FilesToBeDeleted.Add(path);
 }