Пример #1
0
        public static DirectoryDescriptor[] GetDirectoryDescriptors(string path, string pattern = null, bool recursive = false)
        {
            string realPath = PathExpress.ResolvePath(path);
            ICollection <DirectoryDescriptor> results = new List <DirectoryDescriptor>();

            string[] directoryPathArray = pattern == null?Directory.GetDirectories(realPath) : Directory.GetDirectories(realPath, pattern);

            foreach (string directoryPath in directoryPathArray)
            {
                DirectoryDescriptor directoryDescriptor = GetDirectoryDescriptor(directoryPath);
                results.Add(directoryDescriptor);
            }
            if (recursive)
            {
                string[] subDirectoryPaths = Directory.GetDirectories(realPath);
                foreach (string directoryPath in subDirectoryPaths)
                {
                    DirectoryDescriptor[] directoryDescriptors = GetDirectoryDescriptors(directoryPath, pattern, true);
                    foreach (DirectoryDescriptor directoryDescriptor in directoryDescriptors)
                    {
                        results.Add(directoryDescriptor);
                    }
                }
            }
            return(results.ToArray());
        }
Пример #2
0
        public static FileDescriptor SaveToDisk(string path, Stream stream, bool overwrite = false)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (overwrite && File.Exists(realPath))
            {
                File.Delete(realPath);
            }
            using (Stream outputStream = new FileStream(realPath, FileMode.CreateNew))
            {
                if (stream.CanSeek)
                {
                    stream.Position = 0;
                }
                stream.CopyTo(outputStream, TfxCentral.BufferLength);
            }
            FileGetFileDescriptorArgument fileGetFileDecriptorArgument = new FileGetFileDescriptorArgument
            {
                Path             = realPath,
                IncludePathInfo  = true,
                IncludeAttribute = true,
                IncludeMd5       = true,
                IncludeSha1      = true,
                IncludeSha256    = true,
                IncludeSha512    = true
            };
            FileDescriptor fileDescriptor = GetFileDescriptor(fileGetFileDecriptorArgument);

            return(fileDescriptor);
        }
Пример #3
0
        public static FileDescriptor[] GetFileDescriptors(string path, string pattern = null, bool recursive = false)
        {
            string realPath = PathExpress.ResolvePath(path);
            ICollection <FileDescriptor> results = new List <FileDescriptor>();

            string[] filePaths = pattern == null?Directory.GetFiles(realPath) : Directory.GetFiles(realPath, pattern);

            foreach (string filePath in filePaths)
            {
                FileDescriptor fileDescriptor = FileExpress.GetFileDescriptor(filePath);
                results.Add(fileDescriptor);
            }
            if (recursive)
            {
                string[] directoryPaths = Directory.GetDirectories(realPath);
                foreach (string directoryPath in directoryPaths)
                {
                    FileDescriptor[] fileDescriptors = GetFileDescriptors(directoryPath, pattern, true);
                    foreach (FileDescriptor fileDescriptor in fileDescriptors)
                    {
                        results.Add(fileDescriptor);
                    }
                }
            }
            return(results.ToArray());
        }
Пример #4
0
        public static FileDescriptor SaveToDisk(string path, byte[] contents, bool overwrite = false)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (overwrite && File.Exists(realPath))
            {
                File.Delete(realPath);
            }
            using (Stream outputStream = new FileStream(realPath, FileMode.CreateNew))
            {
                outputStream.Write(contents, 0, contents.Length);
            }
            FileGetFileDescriptorArgument fileGetFileDecriptorArgument = new FileGetFileDescriptorArgument
            {
                Path             = realPath,
                IncludePathInfo  = true,
                IncludeAttribute = true,
                IncludeMd5       = true,
                IncludeSha1      = true,
                IncludeSha256    = true,
                IncludeSha512    = true
            };
            FileDescriptor fileDescriptor = GetFileDescriptor(fileGetFileDecriptorArgument);

            return(fileDescriptor);
        }
Пример #5
0
        public static void WriteAllBytes(string path, byte[] contents)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            File.WriteAllBytes(realPath, contents);
        }
Пример #6
0
        public static void PrependAllLines(string path, List <string> contents, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            IList <string> oldContents = new List <string>();

            if (File.Exists(realPath))
            {
                oldContents = File.ReadAllLines(realPath)
                              .ToList();
            }
            IList <string> newContents = new List <string>(contents);

            foreach (string oldContent in oldContents)
            {
                newContents.Add(oldContent);
            }
            if (encoding == null)
            {
                File.WriteAllLines(realPath, newContents);
            }
            else
            {
                File.WriteAllLines(realPath, newContents, encoding);
            }
        }
Пример #7
0
        public static void Copy(string sourcePath, string tartgetPath, bool overwrite = false)
        {
            string realSourcePath = PathExpress.ResolvePath(sourcePath);
            string realTargetPath = PathExpress.ResolvePath(tartgetPath);

            File.Copy(realSourcePath, realTargetPath, overwrite);
        }
Пример #8
0
        public static void RecursiveSetAttributes(string path, FileAttributeSetArgument attr)
        {
            string realPath = PathExpress.ResolvePath(path);

            string[] dirPaths  = Directory.GetDirectories(realPath);
            string[] filePaths = Directory.GetFiles(realPath);
            try
            {
                SetAttribute(realPath, attr);
            }
            catch (Exception ex)
            {
                TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveSetAttributes", string.Empty, ex);
            }
            foreach (string dirPath in dirPaths)
            {
                RecursiveSetAttributes(dirPath, attr);
            }
            foreach (string filePath in filePaths)
            {
                try
                {
                    FileExpress.SetAttribute(filePath, attr);
                }
                catch (Exception ex)
                {
                    TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveSetAttributes", string.Empty, ex);
                }
            }
        }
Пример #9
0
        public static FileStream Create(string path, int bufferSize, FileOptions fileOptions, FileSecurity fileSecurity)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            return(File.Create(path, bufferSize, fileOptions, fileSecurity));
        }
Пример #10
0
        public static FileStream Create(string path, int bufferSize)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            return(File.Create(path, bufferSize));
        }
Пример #11
0
        public static long FileSize(string path)
        {
            string   realPath = PathExpress.ResolvePath(path);
            FileInfo fileInfo = new FileInfo(realPath);
            long     length   = fileInfo.Length;

            return(length);
        }
Пример #12
0
        public static void Move(string sourcePath, string tartgetPath)
        {
            string realSourcePath = PathExpress.ResolvePath(sourcePath);
            string realTargetPath = PathExpress.ResolvePath(tartgetPath);

            DirectoryExpress.CreateParentDirectory(tartgetPath);
            File.Move(realSourcePath, realTargetPath);
        }
Пример #13
0
        public static DirectoryDescriptor GetDirectoryDescriptor(string path, bool includeProperty = false)
        {
            string realPath            = PathExpress.ResolvePath(path);
            string name                = Path.GetFileName(realPath);
            string parentPath          = Path.GetDirectoryName(realPath);
            DirectoryDescriptor result = new DirectoryDescriptor(name, null, parentPath);

            return(result);
        }
Пример #14
0
        public static void Replace(string sourcePath, string tartgetPath, string backupPath, bool ignoreMetadataErrors = false)
        {
            string realSourcePath = PathExpress.ResolvePath(sourcePath);
            string realTargetPath = PathExpress.ResolvePath(tartgetPath);
            string realBackupPath = PathExpress.ResolvePath(backupPath);

            DirectoryExpress.CreateParentDirectory(tartgetPath);
            File.Replace(realSourcePath, realTargetPath, realBackupPath, ignoreMetadataErrors);
        }
Пример #15
0
        public string ReadAllText(string path, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            if (encoding == null)
            {
                return(File.ReadAllText(realPath));
            }
            return(File.ReadAllText(realPath, encoding));
        }
Пример #16
0
        public IEnumerable <string> ReadLines(string path, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            if (encoding == null)
            {
                return(File.ReadLines(realPath));
            }
            return(File.ReadLines(realPath, encoding));
        }
Пример #17
0
        public static void CreateParentDirectory(string path)
        {
            string realPath   = PathExpress.ResolvePath(path);
            string parentPath = Path.GetDirectoryName(realPath);

            if (parentPath == null)
            {
                throw new ArgumentNullException(nameof(parentPath));
            }
            _ = Directory.CreateDirectory(parentPath);
        }
Пример #18
0
        public static void AppendAllLines(string path, IEnumerable <string> contents, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (encoding == null)
            {
                File.AppendAllLines(realPath, contents);
            }
            else
            {
                File.AppendAllLines(realPath, contents, encoding);
            }
        }
Пример #19
0
        public static void WriteAllText(string path, string contents, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            if (encoding == null)
            {
                File.WriteAllText(realPath, contents);
            }
            else
            {
                File.WriteAllText(realPath, contents, encoding);
            }
        }
Пример #20
0
        private static FileStream OpenInner(string path, FileMode fileMode, FileAccess?fileAccess = null, FileShare?fileShare = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            if (fileMode == FileMode.Append || fileMode == FileMode.Create || fileMode == FileMode.CreateNew || fileMode == FileMode.OpenOrCreate)
            {
                DirectoryExpress.CreateParentDirectory(realPath);
            }
            if (fileAccess == null)
            {
                return(File.Open(realPath, fileMode));
            }
            else if (fileShare == null)
            {
                return(File.Open(realPath, fileMode, fileAccess.Value));
            }
            else
            {
                return(File.Open(realPath, fileMode, fileAccess.Value, fileShare.Value));
            }
        }
Пример #21
0
        public static void ExtractManifestResources(Assembly assembly, string source, string target, bool overwrite = false)
        {
            string realTarget = PathExpress.ResolvePath(target);

            if (overwrite == false && File.Exists(realTarget))
            {
                return;
            }
            DirectoryExpress.CreateParentDirectory(realTarget);
            using (Stream reader = assembly.GetManifestResourceStream(source))
            {
                if (reader == null)
                {
                    throw new NullReferenceException(nameof(reader));
                }
                Stream writer = File.Create(realTarget);
                _ = reader.Seek(0, SeekOrigin.Begin);
                reader.CopyTo(writer);
                writer.Close();
            }
        }
Пример #22
0
        public static void SetAttribute(string path, FileAttributeSetArgument attr)
        {
            string         realPath = PathExpress.ResolvePath(path);
            DirectoryInfo  dirInfo  = new DirectoryInfo(realPath);
            FileAttributes dirAttrs = dirInfo.Attributes;

            if (attr.Archive.HasValue)
            {
                dirAttrs = attr.Archive == true ? dirAttrs | FileAttributes.Archive : dirAttrs & ~FileAttributes.Archive;
            }
            if (attr.Hidden.HasValue)
            {
                dirAttrs = attr.Hidden == true ? dirAttrs | FileAttributes.Hidden : dirAttrs & ~FileAttributes.Hidden;
            }
            if (attr.ReadOnly.HasValue)
            {
                dirAttrs = attr.ReadOnly == true ? dirAttrs | FileAttributes.ReadOnly : dirAttrs & ~FileAttributes.ReadOnly;
            }
            if (attr.System.HasValue)
            {
                dirAttrs = attr.System == true ? dirAttrs | FileAttributes.System : dirAttrs & ~FileAttributes.System;
            }
            dirInfo.Attributes = dirAttrs;
            if (dirInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
            {
                return;
            }
            if (attr.CreatedTime.HasValue)
            {
                dirInfo.CreationTime = attr.CreatedTime.Value;
            }
            if (attr.AccessedTime.HasValue)
            {
                dirInfo.LastAccessTime = attr.AccessedTime.Value;
            }
            if (attr.ModifiedTime.HasValue)
            {
                dirInfo.LastWriteTime = attr.ModifiedTime.Value;
            }
        }
Пример #23
0
        public static void PrependAllText(string path, string content, Encoding encoding = null)
        {
            string realPath = PathExpress.ResolvePath(path);

            DirectoryExpress.CreateParentDirectory(realPath);
            string oldContent = string.Empty;

            if (File.Exists(realPath))
            {
                oldContent = File.ReadAllText(realPath);
            }
            string newContent = content + oldContent;

            if (encoding == null)
            {
                File.WriteAllText(realPath, newContent);
            }
            else
            {
                File.WriteAllText(realPath, newContent, encoding);
            }
        }
Пример #24
0
        public static void RecursiveDelete(string path, bool aggressiveMode = false)
        {
            string realPath = PathExpress.ResolvePath(path);

            if (aggressiveMode)
            {
                try
                {
                    Directory.Delete(realPath, true);
                }
                catch (Exception ex)
                {
                    TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveDelete", string.Empty, ex);
                    string[] dirPaths  = Directory.GetDirectories(realPath);
                    string[] filePaths = Directory.GetFiles(realPath);
                    foreach (string dirPath in dirPaths)
                    {
                        RecursiveDelete(dirPath, true);
                    }
                    foreach (string filePath in filePaths)
                    {
                        try
                        {
                            File.Delete(filePath);
                        }
                        catch (Exception exc)
                        {
                            TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveDelete", string.Empty, exc);
                        }
                    }
                }
            }
            else
            {
                Directory.Delete(realPath, true);
            }
        }
Пример #25
0
        public static bool Exist(string path)
        {
            string realPath = PathExpress.ResolvePath(path);

            return(File.Exists(realPath));
        }
Пример #26
0
        public static void CreateDirectory(string path)
        {
            string realPath = PathExpress.ResolvePath(path);

            _ = Directory.CreateDirectory(realPath);
        }
Пример #27
0
        internal static FileDescriptor GetFileDescriptor(FileGetFileDescriptorArgument args)
        {
            string       realPath   = PathExpress.ResolvePath(args.Path);
            string       parent     = Path.GetDirectoryName(realPath);
            string       fullName   = Path.GetFileName(realPath);
            string       name       = Path.GetFileNameWithoutExtension(fullName);
            string       extension  = Path.GetExtension(fullName);
            FileMetadata metadata   = null;
            BytesEx      md5Hash    = null;
            BytesEx      sha1Hash   = null;
            BytesEx      sha256Hash = null;
            BytesEx      sha512Hash = null;

            if (args.IncludeAttribute)
            {
                FileInfo fileInfo     = new FileInfo(realPath);
                bool     isArchive    = fileInfo.Attributes.HasFlag(FileAttributes.Archive);
                bool     isReadOnly   = fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly);
                bool     isHidden     = fileInfo.Attributes.HasFlag(FileAttributes.Hidden);
                bool     isSystem     = fileInfo.Attributes.HasFlag(FileAttributes.System);
                long     length       = fileInfo.Length;
                DateTime createdTime  = fileInfo.CreationTimeUtc;
                DateTime modifiedTime = fileInfo.LastWriteTimeUtc;
                DateTime accessTime   = fileInfo.LastAccessTimeUtc;
                metadata = new FileMetadata(isArchive, isHidden, isReadOnly, isSystem, length, createdTime, accessTime, modifiedTime);
            }
            if (args.IncludeMd5 || args.IncludeSha1 || args.IncludeSha256 || args.IncludeSha512)
            {
                string md5    = "md5";
                string sha1   = "sha1";
                string sha256 = "sha256";
                string sha512 = "sha512";
                Dictionary <string, System.Security.Cryptography.HashAlgorithm> cryptDictionary = new Dictionary <string, System.Security.Cryptography.HashAlgorithm>();
                if (args.IncludeMd5)
                {
                    cryptDictionary[md5] = HashExpress.GetAlgorithm(HashAlgorithm.Md5);
                }
                if (args.IncludeSha1)
                {
                    cryptDictionary[sha1] = HashExpress.GetAlgorithm(HashAlgorithm.Sha1);
                }
                if (args.IncludeSha256)
                {
                    cryptDictionary[sha256] = HashExpress.GetAlgorithm(HashAlgorithm.Sha256);
                }
                if (args.IncludeSha512)
                {
                    cryptDictionary[sha512] = HashExpress.GetAlgorithm(HashAlgorithm.Sha512);
                }
                System.Security.Cryptography.HashAlgorithm[] crypts = cryptDictionary.Values.ToArray();
                using (Stream fileStream = new FileStream(realPath, FileMode.Open))
                {
                    byte[] buffer = new byte[HashExpress.StreamBufferSize];
                    int    read;
                    long   currentPosition = 0L;
                    long   streamLength    = fileStream.Length;
                    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        currentPosition += read;
                        foreach (System.Security.Cryptography.HashAlgorithm crypt in crypts)
                        {
                            if (currentPosition < streamLength)
                            {
                                int _ = crypt.TransformBlock(buffer, 0, read, buffer, 0);
                            }
                            else
                            {
                                byte[] _ = crypt.TransformFinalBlock(buffer, 0, read);
                            }
                        }
                    }
                    if (args.IncludeMd5)
                    {
                        md5Hash = BytesEx.FromBytes(cryptDictionary[md5].Hash);
                    }
                    if (args.IncludeSha1)
                    {
                        sha1Hash = BytesEx.FromBytes(cryptDictionary[sha1].Hash);
                    }
                    if (args.IncludeSha256)
                    {
                        sha256Hash = BytesEx.FromBytes(cryptDictionary[sha256].Hash);
                    }
                    if (args.IncludeSha512)
                    {
                        sha512Hash = BytesEx.FromBytes(cryptDictionary[sha512].Hash);
                    }
                }
            }
            FileDescriptor result = new FileDescriptor(name, extension, parent, metadata, md5Hash, sha1Hash, sha256Hash, sha512Hash);

            return(result);
        }
Пример #28
0
        public byte[] ReadAllBytes(string path)
        {
            string realPath = PathExpress.ResolvePath(path);

            return(File.ReadAllBytes(realPath));
        }