Beispiel #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);
        }
Beispiel #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);
            }
        }