示例#1
0
        public static bool TryCopy(string fullSourceFileName, string fullDestinationFileName, bool allowOverWrite = false)
        {
            try {
                if (fullSourceFileName == fullDestinationFileName)
                {
                    return(false);
                }

                string destinationDirectory = Path.GetDirectoryName(fullDestinationFileName);
                if (!Directory.Exists(destinationDirectory))
                {
                    if (!TryCreateDirectory(destinationDirectory))
                    {
                        return(false);
                    }
                }


                File.Copy(fullSourceFileName, fullDestinationFileName, allowOverWrite);
                return(true);
            }
            catch (Exception ex) {
                string msg = "Exception while copying file " + ex.Message;
                if (fullSourceFileName != null)
                {
                    msg += msg + ". Source: " + fullSourceFileName;
                }
                if (fullDestinationFileName != null)
                {
                    msg += msg + ".DestinationFile:" + fullDestinationFileName;
                }
                ExceptionRecorder.RecordException(msg);
                return(false);
            }
        }
示例#2
0
        // return number of occurrences replaced
        public static void ReplaceInFile(string fullPath, string strToBeReplaced, string replacementString)
        {
            try
            {
                if (!File.Exists(fullPath))
                {
                    return;
                }

                string fileContents = string.Empty;
                using (StreamReader sr = File.OpenText(fullPath))
                {
                    fileContents = sr.ReadToEnd().Replace(strToBeReplaced, replacementString);
                }
                using (var file = System.IO.File.Create(fullPath))
                    using (var sw = new System.IO.StreamWriter(file))
                    {
                        sw.Write(fileContents);
                    }
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception in ReplaceInFile. ", ex);
            }
        }
示例#3
0
        public static void RecordException(string s, Exception ex)
        {
            if (ex == null && s != null)
            {
                RecordException(s);
            }
            string s2 = s;

            if (ex.Message != null)
            {
                s2 += ex.GetType() + ".";
            }
            if (!s2.Contains(ex.Message))
            {
                s2 += ex.Message;
            }
            RecordException(s2);
            if (ex != null && ex.InnerException != null)
            {
                if (ex.InnerException.Message != null)
                {
                    ExceptionRecorder.RecordException("Inner exception:" + ex.InnerException.Message);
                }
            }
        }
示例#4
0
        public static bool CopyFile(string sourceDirectory, string sourceFileName, string destinationDirectory)
        {
            try
            {
                if (!Directory.Exists(destinationDirectory))
                {
                    if (!TryCreateDirectory(destinationDirectory))
                    {
                        return(false);
                    }
                }

                File.Copy(sourceDirectory + "\\" + sourceFileName, destinationDirectory + "\\" + sourceFileName, true);
                return(true);
            }
            catch (Exception ex)
            {
                string msg = "Exception while copying file " + ex.Message;
                if (sourceFileName != null)
                {
                    msg += msg + ". Source: " + sourceFileName;
                }
                if (destinationDirectory != null)
                {
                    msg += msg + ".DestinationDirectory:" + destinationDirectory;
                }
                ExceptionRecorder.RecordException(msg);
                return(false);
            }
        }
示例#5
0
        private void DeleteOldFiles(string directory, int daysToKeepFiles, bool removeDirectory)
        {
            if (string.IsNullOrEmpty(directory))
            {
                return;
            }
            if (daysToKeepFiles < 0)
            {
                return;
            }
            try
            {
                if (!Directory.Exists(directory))
                {
                    return;
                }

                var limitDate = DateTime.Now.AddDays(-daysToKeepFiles);
                FileSystem.DeleteOldFilesFromSubDirectories(directory, FileFilter, daysToKeepFiles, removeDirectory);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception in DeleteOldFiles " + directory + ".", ex);
            }
        }
示例#6
0
 public static string[] TryGetSubDirectories(string path)
 {
     try
     {
         return(Directory.GetDirectories(path));
     }
     catch (Exception ex)
     {
         ExceptionRecorder.RecordException(
             "Exception while trying to read subdirectories directory. " + path + "." + ex.Message, ex);
         return(null);
     }
 }
示例#7
0
 public static string TryGetDirectoryName(string path)
 {
     try
     {
         if (path == string.Empty)
         {
             return(string.Empty);
         }
         return(Path.GetDirectoryName(path));
     }
     catch (Exception ex)
     {
         ExceptionRecorder.RecordException("Invalid file name entered while trying to get path name . ", ex);
         return(string.Empty);
     }
 }
示例#8
0
 public static bool TryDeleteFile(string fileName)
 {
     try
     {
         if (File.Exists(fileName))
         {
             File.Delete(fileName);
         }
         return(true);
     }
     catch (Exception ex)
     {
         ExceptionRecorder.RecordException("Exception while trying to delete file. " + fileName + ".", ex);
         return(false);
     }
 }
示例#9
0
 public static bool CopyFile(string sourceFileNameWithPath, string destinationDirectory)
 {
     try
     {
         string fileName = Path.GetFileName(sourceFileNameWithPath);
         return(CopyFileOverwrite(sourceFileNameWithPath, destinationDirectory, fileName));
     }
     catch (Exception ex)
     {
         string msg = "Exception while copying file " + ex.Message +
                      " 1." + sourceFileNameWithPath +
                      " 2. " + destinationDirectory;
         ExceptionRecorder.RecordException(msg);
         return(false);
     }
 }
示例#10
0
 // return true if no errors took place, even if file was not deleted
 public static bool DeleteOldFile(string fileName, int daysToKeep)
 {
     try
     {
         TimeSpan age = DateTimeEx.Now.Subtract(File.GetLastWriteTime(fileName));
         if (age.TotalDays > daysToKeep)
         {
             return(FileSystem.TryDeleteFile(fileName));
         }
         return(true);
     }
     catch (Exception ex)
     {
         ExceptionRecorder.RecordException("Exception while deleting file " + fileName + ".", ex);
         return(false);
     }
 }
示例#11
0
        public static bool TryMoveFile(string fullSourceFileName, string destinationDirectory, bool overwriteExistingTarget = false)
        {
            try
            {
                if (!Directory.Exists(destinationDirectory))
                {
                    if (!TryCreateDirectory(destinationDirectory))
                    {
                        return(false);
                    }
                }

                string target = destinationDirectory + "\\" + Path.GetFileName(fullSourceFileName);
                if (fullSourceFileName == target)
                {
                    Debug.Assert(false, "trying to copy file over itself");
                    return(true);
                }

                if (overwriteExistingTarget && File.Exists(target))
                {
                    TryDeleteFile(target);
                }


                File.Move(fullSourceFileName, target);
                return(true);
            }
            catch (Exception ex)
            {
                string msg = "Exception while moving file " + ex.Message;
                if (fullSourceFileName != null)
                {
                    msg += msg + ". Source: " + fullSourceFileName;
                }
                if (destinationDirectory != null)
                {
                    msg += msg + ".DestinationDirectory:" + destinationDirectory;
                }
                ExceptionRecorder.RecordException(msg);
                return(false);
            }
        }
示例#12
0
        public static void ClearDirectoryRecursively(string directory)
        {
            int test = 0;

            try
            {
                if (Directory.Exists(directory))
                {
                    test = 1;
                    Directory.Delete(directory, true);
                }
                test = 2;
                Directory.CreateDirectory(directory);
            }
            catch (Exception ex)
            {
                var msg = "Exception while clearing directory " + test + ":" + directory + ":";
                ExceptionRecorder.RecordException(msg, ex);
            }
        }
示例#13
0
        public static void DeleteOldFilesFromSubDirectories(string directoryPath, string filter, int daysToKeep,
                                                            bool removeEmptySubDirectories)
        {
            try
            {
                if (!Directory.Exists(directoryPath))
                {
                    return;
                }

                //System.Threading.Thread.Sleep(0);
                // FileData[] fileDataArray = FastDirectoryEnumerator.GetFiles(directoryPath, filter, SearchOption.TopDirectoryOnly);

                string[] files = Directory.GetFiles(directoryPath, filter, SearchOption.TopDirectoryOnly);


                foreach (var fileName in files)
                {
                    DateTime lastWriteTime = Directory.GetLastAccessTime(fileName);
                    FileSystem.DeleteOldFile(fileName, lastWriteTime, daysToKeep);
                }

                foreach (var subdirectoryName in Directory.GetDirectories(directoryPath))
                {
                    DeleteOldFilesFromSubDirectories(subdirectoryName, filter, daysToKeep, removeEmptySubDirectories);
                }

                if (removeEmptySubDirectories)
                {
                    if (Directory.GetFiles(directoryPath).GetLength(0) == 0 &&
                        Directory.GetDirectories(directoryPath).GetLength(0) == 0)
                    {
                        Directory.Delete(directoryPath);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception while deleting old files : ", ex);
            }
        }
示例#14
0
        public static string GetParentDirectoryName(string directory)
        {
            try
            {
                if (string.IsNullOrEmpty(directory))
                {
                    return(string.Empty);
                }
                if (!Directory.Exists(directory))
                {
                    return(string.Empty);
                }

                return(Directory.GetParent(directory).FullName);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception in GetParentDirectoryName " + directory + "." + ex);
                return(string.Empty);
            }
        }
示例#15
0
        public static bool DeleteFilesFromDirectory(string directory)
        {
            try
            {
                if (!Directory.Exists(directory))
                {
                    return(true);
                }

                foreach (var file in Directory.GetFiles(directory))
                {
                    File.Delete(file);
                }
                return(true);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception while deleting files : ", ex);
                return(false);
            }
        }
示例#16
0
        public static bool TryCreateDirectory(string path)
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    ExceptionRecorder.RecordException(
                        "Null or empty path name entred while trying to create directory. ");
                    return(false);
                }

                if (Directory.Exists(path))
                {
                    return(true);
                }

                if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1)
                {
                    int index = path.IndexOfAny(Path.GetInvalidPathChars());
                    ExceptionRecorder.RecordException("Path name contains invalid character:" + path[index]);
                    return(false);
                }

                Directory.CreateDirectory(path);
                return(true);
            }
            catch (UnauthorizedAccessException ex)
            {
                ExceptionRecorder.RecordException(
                    "UnauthorizedAccessException while trying to create directory. " + path + "." + ex.Message, ex);
                return(false);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException(
                    "Exception while trying to create directory. " + path + "." + ex.Message, ex);
                return(false);
            }
        }
示例#17
0
        public static bool TryDeleteDirectory(string path)
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    ExceptionRecorder.RecordException(
                        "Null or empty path name entered while trying to delete directory. ");
                    return(false);
                }

                if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1)
                {
                    int index = path.IndexOfAny(Path.GetInvalidPathChars());
                    ExceptionRecorder.RecordException("Path name contains invalid character:" + path[index]);
                    return(false);
                }
                const bool recursive = true;
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, recursive);
                }

                return(true);
            }
            catch (UnauthorizedAccessException ex)
            {
                ExceptionRecorder.RecordException(
                    "UnauthorizedAccessException while trying to delete directory. Check if Explorer or some other program is accessing directory, subdirectory or any file being deleted. Path: " + path + "." + ex.Message, ex);
                return(false);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException(
                    "Exception while trying to delete directory. " + path + "." + ex.Message, ex);
                return(false);
            }
        }
示例#18
0
        public static bool InitialiseFromString(string gapString, out Gap gap)
        {
            gap = new Gap();
            try
            {
                if (gapString == null)
                {
                    return(false);
                }
                if (gapString.Length != gap.ToString().Length)
                {
                    return(false);
                }

                string   start = gapString.Substring(0, DateTimeEx.FormatStringDefault.Length);
                DateTime dateStart;
                if (!DateTimeEx.TryParseStandardFormat(start, out dateStart))
                {
                    return(false);
                }

                string   end = gapString.Substring(DateTimeEx.FormatStringDefault.Length + 3);
                DateTime dateEnd;
                if (!DateTimeEx.TryParseStandardFormat(end, out dateEnd))
                {
                    return(false);
                }
                gap = new Gap(dateStart, dateEnd);
                return(true);
            }
            catch (Exception ex)
            {
                ExceptionRecorder.RecordException("Exception in Gap::InitialiseFromString ", ex);
                return(false);
            }
        }
示例#19
0
        public static bool CopyFileDoNotOverwrite(string sourceFileNameWithPath, string destinationDirectory)
        {
            try
            {
                if (!Directory.Exists(destinationDirectory))
                {
                    if (!TryCreateDirectory(destinationDirectory))
                    {
                        return(false);
                    }
                }

                if (!File.Exists(sourceFileNameWithPath))
                {
                    return(false);
                }

                string fileName            = Path.GetFileName(sourceFileNameWithPath);
                string destinationFileName = destinationDirectory + "\\" + fileName;
                while (File.Exists(destinationFileName))
                {
                    destinationFileName = destinationDirectory + "\\" + Path.GetRandomFileName() + fileName;
                }

                File.Copy(sourceFileNameWithPath, destinationFileName, true);
                return(true);
            }
            catch (Exception ex)
            {
                string msg = "Exception while copying file " + ex.Message +
                             " 1." + sourceFileNameWithPath +
                             " 2. " + destinationDirectory;
                ExceptionRecorder.RecordException(msg);
                return(false);
            }
        }
示例#20
0
        public static bool TryGetDirectoryName(string path, out string directory)
        {
            directory = string.Empty;
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            try
            {
                directory = System.IO.Path.GetDirectoryName(path);
                return(true);
            }
            catch (System.ArgumentException ex)
            {
                ExceptionRecorder.RecordException("Argument exception", ex);
                return(false);
            }
            catch (System.IO.PathTooLongException ex)
            {
                ExceptionRecorder.RecordException("PathTooLongException exception", ex);
                return(false);
            }
        }
示例#21
0
        public static bool CopyFileOverwrite(string sourceFileNameWithPath, string destinationDirectory, string destinationFileName)
        {
            try
            {
                if (!Directory.Exists(destinationDirectory))
                {
                    if (!TryCreateDirectory(destinationDirectory))
                    {
                        return(false);
                    }
                }

                if (!File.Exists(sourceFileNameWithPath))
                {
                    return(false);
                }

                string target = destinationDirectory + "\\" + destinationFileName;
                if (target == sourceFileNameWithPath)
                {
                    return(false);
                }

                File.Copy(sourceFileNameWithPath, target, true);
                return(true);
            }
            catch (Exception ex)
            {
                string msg = "Exception while copying file " + ex.Message +
                             " 1." + sourceFileNameWithPath +
                             " 2. " + destinationDirectory +
                             " 3. " + destinationFileName;
                ExceptionRecorder.RecordException(msg);
                return(false);
            }
        }
示例#22
0
 public static void RecordException(string message)
 {
     ExceptionRecorder.RecordException(message);
 }
示例#23
0
 public static void HandleException(Exception ex)
 {
     ExceptionRecorder.RecordException(ex);
 }
示例#24
0
 public static void HandleException(Exception ex, string message)
 {
     ExceptionRecorder.RecordException(message, ex);
 }