Пример #1
0
        private static void BackupFile(string strFileToBackup, string strBaseTargetDirectory)
        {
            string strBackedUpDirectory = Path.GetFileNameWithoutExtension(strFileToBackup) + "_" + Path.GetExtension(strFileToBackup);

            strBackedUpDirectory = Path.Combine(strBaseTargetDirectory, strBackedUpDirectory);

            bool blnBackedUpDirectoryExists = DirectoryManager.Exists(strBackedUpDirectory);

            if (blnBackedUpDirectoryExists == false)
            {
                try
                {
                    DirectoryManager.Create(strBackedUpDirectory);
                }
                catch (Exception objException)
                {
                    throw new Exception("Backup directory '" + strBackedUpDirectory + "' could not be created.", objException);
                }
            }

            string strUniqueValue      = DateTime.Now.Ticks.ToString();
            string strBackedUpFileName = Path.GetFileNameWithoutExtension(strFileToBackup) + "_" + strUniqueValue + Path.GetExtension(strFileToBackup);

            strBackedUpFileName = Path.Combine(strBackedUpDirectory, strBackedUpFileName);

            FileManager.Copy(strFileToBackup, strBackedUpFileName);
        }
Пример #2
0
        public static void Copy(string strSourceDirectory, string strTargetDirectory, string strFilter, bool blnCopyOnlyIfNewer, bool blnBackupTarget)
        {
            bool blnSourceDirectoryExists = DirectoryManager.Exists(strSourceDirectory);

            if (blnSourceDirectoryExists == false)
            {
                throw new Exception("Source directory '" + strSourceDirectory + "' does not exist.");
            }
            DirectoryInfo objSourceDirectory = new DirectoryInfo(strSourceDirectory);

            bool blnTargetDirectoryExists = DirectoryManager.Exists(strTargetDirectory);

            if (blnTargetDirectoryExists == false)
            {
                try
                {
                    DirectoryManager.Create(strTargetDirectory);
                }
                catch (Exception objException)
                {
                    throw new Exception("Target directory '" + strTargetDirectory + "' could not be created.", objException);
                }
            }
            DirectoryInfo objTargetDirectory = new DirectoryInfo(strTargetDirectory);

            FileInfo[] objFilesToCopy = objSourceDirectory.GetFiles(strFilter);
            foreach (FileInfo objFileToCopy in objFilesToCopy)
            {
                FileManager.Copy(objFileToCopy.FullName, strTargetDirectory, blnCopyOnlyIfNewer, blnBackupTarget);
            }
        }
Пример #3
0
        public static void Delete(string strDirectoryPath, bool blnRenameOnFail)
        {
            bool blnDirectoryExists = DirectoryManager.Exists(strDirectoryPath);

            if (blnDirectoryExists == false)
            {
                return;
            }

            Exception objActiveException = null;

            try
            {
                FileAttributes enuFileAttributes = DirectoryManager.GetAttributes(strDirectoryPath);
                if ((enuFileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    DirectoryManager.SetAttributes(strDirectoryPath, FileAttributes.Normal);
                }
            }
            catch (Exception objException)
            {
                objActiveException = objException;
            }

            if (objActiveException == null)
            {
                try
                {
                    Directory.Delete(strDirectoryPath, true);
                }
                catch (Exception objException)
                {
                    objActiveException = objException;
                }
            }

            if ((objActiveException != null) && (blnRenameOnFail == true))
            {
                objActiveException = null;
                string strRenameDirectoryPath = DirectoryManager.CreateDeleteDirectoryName(strDirectoryPath);

                try
                {
                    DirectoryManager.Move(strDirectoryPath, strRenameDirectoryPath, "*.*");
                }
                catch (Exception objException)
                {
                    string strErrorMessage = "Error while attempting to rename (move) directory '" + strDirectoryPath + "' for deletion:\r\n";
                    objActiveException = new Exception(strErrorMessage, objException);
                }
            }

            if (objActiveException != null)
            {
                throw objActiveException;
            }
        }
Пример #4
0
        public static void RemoveDeletedItems(string strDirectoryPath, SearchOption enuDeleteScope)
        {
            bool blnDirectoryExists = DirectoryManager.Exists(strDirectoryPath);

            if (blnDirectoryExists == true)
            {
                string strSearchPattern = "*" + FileManager.DeleteExtension;

                try
                {
                    string[] strDeletedDirectories = Directory.GetDirectories(strDirectoryPath, strSearchPattern, enuDeleteScope);
                    foreach (string strDeletedDirectory in strDeletedDirectories)
                    {
                        try
                        {
                            DirectoryManager.Delete(strDeletedDirectory, false);
                        }
                        catch
                        {
                            /// Ignore any errors since we are trying to remove files that
                            /// are marked for deletion.
                            ///
                        }
                    }
                }
                catch
                {
                    /// Ignore any errors since we are trying to remove files that
                    /// are marked for deletion.
                    ///
                }

                try
                {
                    string[] strDeletedFiles = Directory.GetFiles(strDirectoryPath, strSearchPattern, SearchOption.TopDirectoryOnly);
                    foreach (string strDeletedFile in strDeletedFiles)
                    {
                        try
                        {
                            FileManager.Delete(strDeletedFile, false);
                        }
                        catch
                        {
                            /// Ignore any errors since we are trying to remove files that
                            /// are marked for deletion.
                            ///
                        }
                    }
                }
                catch
                {
                    /// Ignore any errors since we are trying to remove files that
                    /// are marked for deletion.
                    ///
                }
            }
        }
Пример #5
0
        public static void Create(string strDirectoryPath)
        {
            bool blnDirectoryExists = DirectoryManager.Exists(strDirectoryPath);

            if (blnDirectoryExists == false)
            {
                Directory.CreateDirectory(strDirectoryPath);
            }
        }
Пример #6
0
        public static void SetAttributes(string strDirectoryPath, FileAttributes enuFileAttributes)
        {
            bool blnExists = DirectoryManager.Exists(strDirectoryPath);

            if (blnExists == true)
            {
                DirectoryInfo objDirectoryInfo = new DirectoryInfo(strDirectoryPath);
                objDirectoryInfo.Attributes = enuFileAttributes;
            }
        }
Пример #7
0
        private static string CreateDeleteDirectoryName(string strDirectoryPath)
        {
            int    intIndex = 0;
            string strRenameDirectoryPath = strDirectoryPath + FileManager.DeleteExtension;

            while (DirectoryManager.Exists(strRenameDirectoryPath) == true)
            {
                intIndex++;
                strRenameDirectoryPath = strDirectoryPath + "." + intIndex.ToString() + FileManager.DeleteExtension;
            }

            return(strRenameDirectoryPath);
        }
Пример #8
0
        public static void Copy(string strFilePath, string strTargetDirectory, bool blnCopyOnlyIfNewer, bool blnBackupTarget)
        {
            bool blnFileExists = FileManager.Exists(strFilePath);

            if (blnFileExists == false)
            {
                throw new Exception("Source file '" + strFilePath + "' does not exist.");
            }
            FileInfo objFileToCopy = new FileInfo(strFilePath);

            bool blnTargetDirectoryExists = DirectoryManager.Exists(strTargetDirectory);

            if (blnTargetDirectoryExists == false)
            {
                try
                {
                    DirectoryManager.Create(strTargetDirectory);
                }
                catch (Exception objException)
                {
                    throw new Exception("Target directory '" + strTargetDirectory + "' could not be created.", objException);
                }
            }
            DirectoryInfo objTargetDirectory = new DirectoryInfo(strTargetDirectory);

            string strTargetFilePath   = Path.Combine(strTargetDirectory, objFileToCopy.Name);
            bool   blnTargetFileExists = FileManager.Exists(strTargetFilePath);

            if (blnTargetFileExists == true)
            {
                FileInfo objTargetFileInfo = new FileInfo(strTargetFilePath);
                if (objFileToCopy.LastWriteTime.Ticks < objTargetFileInfo.LastWriteTime.Ticks)
                {
                    if (blnBackupTarget == true)
                    {
                        BackupFile(objFileToCopy.FullName, strTargetDirectory);
                    }

                    if (blnCopyOnlyIfNewer == false)
                    {
                        objTargetFileInfo.Attributes = FileAttributes.Normal;
                        objTargetFileInfo.Delete();
                        File.Copy(objFileToCopy.FullName, strTargetFilePath);
                    }
                }
                else
                {
                    if (blnBackupTarget == true)
                    {
                        BackupFile(strTargetFilePath, strTargetDirectory);
                    }

                    objTargetFileInfo.Attributes = FileAttributes.Normal;
                    objTargetFileInfo.Delete();
                    File.Copy(objFileToCopy.FullName, strTargetFilePath);
                }
            }
            else
            {
                File.Copy(objFileToCopy.FullName, strTargetFilePath);
            }
        }