예제 #1
0
        private static IEnumerable <string> EnumerateFiles(string path, string searchPattern, bool useSearchOption, SearchOption searchOption)
        {
            string methodName       = StackTraceHelper.GetMethod(StackTraceCaller.This).Name;
            bool   logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            IEnumerable <string> output = new List <string>();

            if (Directory.Exists(path))
            {
                try
                {
                    if (!string.IsNullOrEmpty(searchPattern))
                    {
                        output = useSearchOption
                                     ? Directory.EnumerateFiles(path, searchPattern, searchOption)
                                     : Directory.EnumerateFiles(path, searchPattern);
                    }
                    else
                    {
                        output = Directory.EnumerateFiles(path);
                    }

                    // HARD-CODED constant
                    Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, "List retrieved succesfully.");
                }
                catch (ArgumentNullException argumentNullException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                }
                catch (ArgumentException argumentException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                }
                catch (IOException ioException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                }
                catch (SecurityException securityException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                }
            }

            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }
예제 #2
0
        public static byte[] ReadFileAllBytes(string path, Encoding encoding)
        {
            string methodName = StackTraceHelper.GetMethod(StackTraceCaller.This)
                                .Name;
            bool logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            byte[] output  = null;
            bool   isExist = File.Exists(path);

            if (isExist)
            {
                try
                {
                    output = File.ReadAllBytes(path);
                    // HARD-CODED constant
                    Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, string.Format(CultureInfo.InvariantCulture, "Read complete {0}", path));
                }
                catch (ArgumentNullException argumentNullException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                }
                catch (ArgumentException argumentException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                }
                catch (SecurityException securityException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                }
                catch (FileNotFoundException fileNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, fileNotFoundException.Message);
                }
                catch (IOException ioException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                }
                catch (NotSupportedException notSupportedException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, notSupportedException.Message);
                }
            }

            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }
예제 #3
0
        /// <summary>
        /// 获取当前运行的代码信息作为日志的Tag
        /// 格式为 文件名-方法名-代码所在行数
        /// </summary>
        /// <param name="skipFrames"></param>
        /// <returns></returns>
        public static string GetTag(int skipFrames = 2)
        {
            StackFrame frame      = new StackFrame(skipFrames, true);
            var        lineNumber = StackTraceHelper.GetLineNumber(frame);
            var        method     = StackTraceHelper.GetMethod(frame);
            var        fileName   = StackTraceHelper.GetFileNameWithoutPath(frame);

            return(string.Format("{0}-{1}-{2}", fileName, method, lineNumber));
        }
예제 #4
0
        public static bool WriteFileAllBytes(string path, byte[] bytes)
        {
            string methodName = StackTraceHelper.GetMethod(StackTraceCaller.This)
                                .Name;
            bool logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            var output = false;

            try
            {
                File.WriteAllBytes(path, bytes);
                output = true;

                // HARD-CODED constant
                Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, string.Format(CultureInfo.InvariantCulture, "Write complete {0}", path));
            }
            catch (ArgumentNullException argumentNullException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
            }
            catch (ArgumentException argumentException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
            }
            catch (UnauthorizedAccessException unauthorizedAccessException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
            }
            catch (SecurityException securityException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
            }
            catch (DirectoryNotFoundException directoryNotFoundException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
            }
            catch (FileNotFoundException fileNotFoundException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, fileNotFoundException.Message);
            }
            catch (IOException ioException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
            }
            catch (NotSupportedException notSupportedException)
            {
                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, notSupportedException.Message);
            }


            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }
예제 #5
0
        public static bool CopyFile(string sourceFile, string destinationFile, bool overwrite)
        {
            string methodName = StackTraceHelper.GetMethod(StackTraceCaller.This)
                                .Name;
            bool logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            var  output  = false;
            bool isExist = File.Exists(sourceFile);

            if (isExist)
            {
                try
                {
                    File.Copy(sourceFile, destinationFile, overwrite);
                    output = true;

                    // HARD-CODED constant
                    Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, string.Format(CultureInfo.InvariantCulture, "Copied from {0} to {1}, overwrite {2}", sourceFile, destinationFile, overwrite));
                }
                catch (ArgumentNullException argumentNullException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                }
                catch (ArgumentException argumentException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                }
                catch (IOException ioException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                }
                catch (NotSupportedException notSupportedException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, notSupportedException.Message);
                }
            }

            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }
예제 #6
0
        public void GetMethodFromStack_UsingStackTraceCaller()
        {
            // Arrange
            string expectedValue = MethodBase.GetCurrentMethod()
                                   .Name;

            // Act
            MethodBase value       = StackTraceHelper.GetMethod(StackTraceCaller.This);
            string     actualValue = value.Name;

            // Assert
            Assert.AreEqual(expectedValue, actualValue);
        }
예제 #7
0
        public static bool DeleteFile(string path)
        {
            string methodName = StackTraceHelper.GetMethod(StackTraceCaller.This)
                                .Name;
            bool logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            var  output  = false;
            bool isExist = File.Exists(path);

            if (isExist)
            {
                try
                {
                    File.Delete(path);
                    output = true;
                    // HARD-CODED constant
                    Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, string.Format(CultureInfo.InvariantCulture, "Deleted {0}", path));
                }
                catch (ArgumentNullException argumentNullException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                }
                catch (ArgumentException argumentException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                }
                catch (UnauthorizedAccessException unauthorizedAccessException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                }
                catch (IOException ioException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                }
                catch (NotSupportedException notSupportedException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, notSupportedException.Message);
                }
            }

            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }
예제 #8
0
        /// <summary>
        ///     Deletes the given directory, including the files in it
        /// </summary>
        /// <param name="path"></param>
        public static bool DeleteDirectory(string path)
        {
            var output = false;

            string methodName       = StackTraceHelper.GetMethod(StackTraceCaller.This).Name;
            bool   logicalOperation = TraceHelper.StartLogicalOperation(methodName);

            if (Directory.Exists(path))
            {
                try
                {
                    var proceed       = false;
                    var directoryInfo = new DirectoryInfo(path);
                    foreach (FileInfo file in directoryInfo.GetFiles())
                    {
                        try
                        {
                            FileHelper.DeleteFile(file.FullName);
                            proceed = true;
                        }
                        catch (SecurityException securityException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                            break;
                        }
                        catch (PathTooLongException pathTooLongException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, pathTooLongException.Message);
                            break;
                        }
                    }
                    if (proceed)
                    {
                        proceed = false;
                        try
                        {
                            DirectoryInfo[] directoriesInfo = directoryInfo.GetDirectories();
                            foreach (DirectoryInfo directory in directoriesInfo)
                            {
                                directory.Delete(true);
                            }
                            proceed = true;
                        }
                        catch (UnauthorizedAccessException unauthorizedAccessException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                        }
                        catch (SecurityException securityException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                        }
                        catch (DirectoryNotFoundException directoryNotFoundException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                        }
                        catch (IOException ioException)
                        {
                            Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                        }
                        if (proceed)
                        {
                            try
                            {
                                Directory.Delete(path);
                            }
                            catch (ArgumentNullException argumentNullException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                            }
                            catch (ArgumentException argumentException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                            }
                            catch (PathTooLongException pathTooLongException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, pathTooLongException.Message);
                            }
                            catch (UnauthorizedAccessException unauthorizedAccessException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, unauthorizedAccessException.Message);
                            }
                            catch (DirectoryNotFoundException directoryNotFoundException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                            }
                            catch (SecurityException securityException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                            }
                            catch (IOException ioException)
                            {
                                Tracer.Instance.TraceEvent(TraceEventType.Error, 0, ioException.Message);
                            }
                            output = true;

                            // HARD-CODED constant
                            Tracer.Instance.TraceEvent(TraceEventType.Verbose, 0, string.Format(CultureInfo.InvariantCulture, "Deleted {0}", path));
                        }
                    }
                }
                catch (ArgumentNullException argumentNullException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentNullException.Message);
                }
                catch (ArgumentException argumentException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, argumentException.Message);
                }
                catch (PathTooLongException pathTooLongException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, pathTooLongException.Message);
                }
                catch (SecurityException securityException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, securityException.Message);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Tracer.Instance.TraceEvent(TraceEventType.Error, 0, directoryNotFoundException.Message);
                }
            }

            if (logicalOperation)
            {
                TraceHelper.StopLogicalOperation();
            }

            return(output);
        }