コード例 #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
ファイル: DirectoryManager.cs プロジェクト: wuhaiying83/EMR
        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 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);
            }
        }