Exemplo n.º 1
0
        public static void Delete(string strFilePath, bool blnRenameOnFail)
        {
            bool blnFileExists = FileManager.Exists(strFilePath);

            if (blnFileExists == false)
            {
                return;
            }

            Exception objActiveException = null;

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

            if (objActiveException == null)
            {
                try
                {
                    File.Delete(strFilePath);
                    OnFileDeleted(strFilePath);
                }
                catch (Exception objException)
                {
                    objActiveException = objException;
                }
            }

            if ((objActiveException != null) && (blnRenameOnFail == true))
            {
                objActiveException = null;

                string strRenameFilePath = CreateDeleteFileName(strFilePath);

                try
                {
                    File.Move(strFilePath, strRenameFilePath);
                    OnDeletedFileRenamed(strFilePath, strRenameFilePath);
                }
                catch (Exception objException)
                {
                    string strErrorMessage = "Error while attempting to rename (move) file '" + strFilePath + "' for deletion:\r\n";
                    objActiveException = new Exception(strErrorMessage, objException);
                }
            }

            if (objActiveException != null)
            {
                throw objActiveException;
            }
        }
Exemplo n.º 2
0
        public static void SetAttributes(string strFilePath, FileAttributes enuFileAttributes)
        {
            bool blnExists = FileManager.Exists(strFilePath);

            if (blnExists == true)
            {
                File.SetAttributes(strFilePath, enuFileAttributes);
            }
        }
Exemplo n.º 3
0
        private static string CreateDeleteFileName(string strFilePath)
        {
            int    intIndex          = 0;
            string strRenameFilePath = strFilePath + DeleteExtension;

            while (FileManager.Exists(strRenameFilePath) == true)
            {
                intIndex++;
                strRenameFilePath = strFilePath + "." + intIndex.ToString() + DeleteExtension;
            }

            return(strRenameFilePath);
        }
Exemplo n.º 4
0
        public static FileAttributes GetAttributes(string strFilePath, FileAttributes enuDefaultFileAttributes)
        {
            FileAttributes enuFileAttributes = enuDefaultFileAttributes;

            bool blnExists = FileManager.Exists(strFilePath);

            if (blnExists == true)
            {
                enuFileAttributes = File.GetAttributes(strFilePath);
            }

            return(enuFileAttributes);
        }
Exemplo n.º 5
0
        public static long Size(string strFilePath)
        {
            long lngSize = 0;

            bool blnFileExists = FileManager.Exists(strFilePath);

            if (blnFileExists == true)
            {
                FileInfo objFileInfo = new FileInfo(strFilePath);
                lngSize = objFileInfo.Length;
            }

            return(lngSize);
        }
Exemplo n.º 6
0
        public static FileAttributes GetAttributes(string strDirectoryPath, FileAttributes enuDefaultFileAttributes)
        {
            FileAttributes enuFileAttributes = enuDefaultFileAttributes;

            bool blnExists = FileManager.Exists(strDirectoryPath);

            if (blnExists == true)
            {
                DirectoryInfo objDirectoryInfo = new DirectoryInfo(strDirectoryPath);
                enuFileAttributes = objDirectoryInfo.Attributes;
            }

            return(enuFileAttributes);
        }
Exemplo n.º 7
0
        public static void Copy(string strSourceFilePath, string strTargetFilePath)
        {
            bool blnSourceFileExists = FileManager.Exists(strSourceFilePath);

            if (blnSourceFileExists == false)
            {
                string strErrorMessage = "The source file '" + strSourceFilePath + "' does not exist.";
                throw new Exception(strErrorMessage);
            }

            bool blnTargetFileExists = FileManager.Exists(strTargetFilePath);

            if (blnTargetFileExists == true)
            {
                Delete(strTargetFilePath, true);
            }

            File.Copy(strSourceFilePath, strTargetFilePath);
        }
Exemplo n.º 8
0
        public static FileToken Load(string strFilePath, string strRelativePath)
        {
            if (strFilePath == null)
            {
                throw new ArgumentNullException("strFilePath", "A valid non-null string is required.");
            }
            if (FileManager.Exists(strFilePath) == false)
            {
                throw new FileNotFoundException("Unable to generate FileToken because the file does not exist.", strFilePath);
            }

            FileToken objFileToken = null;

            if (FileManager.Exists(strFilePath) == true)
            {
                string strHash = string.Empty;

                FileInfo objFileInfo = new FileInfo(strFilePath);
                try
                {
                    using (Stream objFileSteam = objFileInfo.OpenRead())
                    {
                        HashBase.Default.ComputeHash(objFileSteam, out strHash);
                    }

                    FileVersionInfo objFileVersionInfo = FileVersionInfo.GetVersionInfo(strFilePath);
                    VersionToken    objVersionToken    = new VersionToken(objFileVersionInfo.FileBuildPart, objFileVersionInfo.FileMajorPart, objFileVersionInfo.FileMinorPart, 0);

                    string strRelativeFileName = Path.Combine(strRelativePath, objFileInfo.Name);
                    objFileToken = new FileToken(strFilePath, strRelativeFileName, strHash, objFileInfo.Length, objVersionToken);
                }
                catch
                {}
            }

            return(objFileToken);
        }
Exemplo n.º 9
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);
            }
        }