예제 #1
0
 /// <summary>
 /// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor.
 /// The <see cref="System.IO.FileStream"/> will close the handle.
 /// </summary>
 public SafeFileHandle CreateHandle(
     CreationDisposition creationDisposition,
     FileAccess fileAccess,
     FileShare fileShare)
 {
     return(ZlpIOHelper.CreateFileHandle(_path, creationDisposition, fileAccess, fileShare));
 }
예제 #2
0
        public static string GetLongPath(string path)
        {
            path = ZlpIOHelper.CheckAddLongPathPrefix(path);

            // --
            // Determine length.

            var sb = new StringBuilder();

            var realLength = PInvokeHelper.GetLongPathName(path, sb, 0);

            // --

            sb.Length  = (int)realLength;
            realLength = PInvokeHelper.GetLongPathName(path, sb, (uint)sb.Length);

            if (realLength <= 0)
            {
                var lastWin32Error = Marshal.GetLastWin32Error();
                throw new Win32Exception(
                          lastWin32Error,
                          $"Error {lastWin32Error} getting long path for '{path}': {ZlpIOHelper.CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)}");
            }
            else
            {
                return(sb.ToString());
            }
        }
        public static void SafeCheckCreateDirectory(
            string folderPath)
        {
            Trace.TraceInformation(@"About to safe check-create folder '{0}'.", folderPath);

            if (!string.IsNullOrEmpty(folderPath) && !SafeDirectoryExists(folderPath))
            {
                try
                {
                    ZlpIOHelper.CreateDirectory(folderPath);
                }
                catch (UnauthorizedAccessException x)
                {
                    Trace.TraceWarning(
                        @"Caught UnauthorizedAccessException while safe check-creating folder '{0}'. {1}", folderPath,
                        x.Message);
                }
                catch (Win32Exception x)
                {
                    Trace.TraceWarning(@"Caught IOException while safe check-creating folder '{0}'. {1}", folderPath,
                                       x.Message);
                }
            }
            else
            {
                Trace.TraceInformation(
                    @"Not safe check-creating folder '{0}', because the folder is null or already exists.", folderPath);
            }
        }
예제 #4
0
        public static string GetFullPath(string path)
        {
            path = ZlpIOHelper.CheckAddLongPathPrefix(path);

            // --
            // Determine length.

            var sb = new StringBuilder();

            var realLength = PInvokeHelper.GetFullPathName(path, 0, sb, IntPtr.Zero);

            // --

            sb.Length  = realLength;
            realLength = PInvokeHelper.GetFullPathName(path, sb.Length, sb, IntPtr.Zero);

            if (realLength <= 0)
            {
                var lastWin32Error = Marshal.GetLastWin32Error();
                throw new Win32Exception(
                          lastWin32Error,
                          string.Format(
                              "Error {0} getting full path for '{1}': {2}",
                              lastWin32Error,
                              path,
                              ZlpIOHelper.CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
            }
            else
            {
                return(sb.ToString());
            }
        }
예제 #5
0
        public ZlpDirectoryInfo CreateSubdirectory(string name)
        {
            var path = ZlpPathHelper.Combine(FullName, name);

            ZlpIOHelper.CreateDirectory(path);
            return(new ZlpDirectoryInfo(path));
        }
예제 #6
0
        public static bool IsAbsolutePath(
            string path)
        {
            path = path.Replace('/', '\\');
            path = ZlpIOHelper.ForceRemoveLongPathPrefix(path);

            if (path.Length < 2)
            {
                return(false);
            }
            else if (path.Substring(0, 2) == @"\\")
            {
                // UNC.
                return(IsUncPath(path));
            }
            else if (path.Substring(1, 1) == @":")
            {
                // "C:"
                return(IsDriveLetterPath(path));
            }
            else
            {
                return(false);
            }
        }
예제 #7
0
 public System.IO.FileStream OpenWrite()
 {
     return(new System.IO.FileStream(
                ZlpIOHelper.CreateFileHandle(
                    _path,
                    CreationDisposition.OpenAlways,
                    FileAccess.GenericRead | FileAccess.GenericWrite,
                    FileShare.Read | FileShare.Write),
                System.IO.FileAccess.ReadWrite));
 }
예제 #8
0
 public System.IO.FileStream OpenRead()
 {
     return(new System.IO.FileStream(
                ZlpIOHelper.CreateFileHandle(
                    FullName,
                    CreationDisposition.OpenAlways,
                    FileAccess.GenericRead,
                    FileShare.Read),
                System.IO.FileAccess.Read));
 }
예제 #9
0
        public static string GetPathRoot(string path)
        {
            if (IsNullOrEmpty(path))
            {
                return(path);
            }
            else
            {
                path = ZlpIOHelper.ForceRemoveLongPathPrefix(path);

                return(GetDriveOrShare(path));
            }
        }
        public static void SafeCopyFile(
            string sourcePath,
            string dstFilePath,
            bool overwrite = true)
        {
            Trace.TraceInformation(@"About to safe-copy file from '{0}' to '{1}' " +
                                   @"with overwrite = '{2}'.", sourcePath, dstFilePath, overwrite);

            if (sourcePath == null || dstFilePath == null)
            {
                Trace.TraceInformation(
                    string.Format(
                        @"Source file path or destination file path does not exist. " +
                        @"Not copying."
                        ));
            }
            else
            {
                if (string.Compare(sourcePath, dstFilePath, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    Trace.TraceInformation(@"Source path and destination path are the same: " +
                                           @"'{0}' is '{1}'. Not copying.", sourcePath, dstFilePath);
                }
                else
                {
                    if (SafeFileExists(sourcePath))
                    {
                        if (overwrite)
                        {
                            SafeDeleteFile(dstFilePath);
                        }

                        var d = ZlpPathHelper.GetDirectoryPathNameFromFilePath(dstFilePath);

                        if (!ZlpIOHelper.DirectoryExists(d))
                        {
                            Trace.TraceInformation(@"Creating non-existing folder '{0}'.", d);
                            ZlpIOHelper.CreateDirectory(d);
                        }

                        ZlpIOHelper.CopyFile(sourcePath, dstFilePath, overwrite);
                    }
                    else
                    {
                        Trace.TraceInformation(@"Source file path to copy does not exist: '{0}'.", sourcePath);
                    }
                }
            }
        }
예제 #11
0
        public static void SafeDeleteDirectory(
            string folderPath)
        {
#if WANT_TRACE
            Trace.TraceInformation(@"About to safe-delete directory '{0}'.", folderPath);
#endif

            if (!string.IsNullOrEmpty(folderPath) && SafeDirectoryExists(folderPath))
            {
                try
                {
                    ZlpIOHelper.DeleteDirectory(folderPath, true);
                }
                catch (Win32Exception x)
                {
                    var newFilePath = $@"{folderPath}.{Guid.NewGuid():B}.deleted";

#if WANT_TRACE
                    Trace.TraceWarning(@"Caught IOException while deleting directory '{0}'. " +
                                       @"Renaming now to '{1}'. {2}", folderPath, newFilePath, x.Message);
#endif

                    try
                    {
                        ZlpIOHelper.MoveDirectory(
                            folderPath,
                            newFilePath);
                    }
                    catch (Win32Exception x2)
                    {
#if WANT_TRACE
                        Trace.TraceWarning(@"Caught IOException while renaming upon failed deleting directory '{0}'. " +
                                           @"Renaming now to '{1}'. {2}", folderPath, newFilePath, x2.Message);
#endif
                    }
                }
            }
            else
            {
#if WANT_TRACE
                Trace.TraceInformation(@"Not safe-deleting directory '{0}', " +
                                       @"because the directory does not exist.", folderPath);
#endif
            }
        }
        public static void SafeMoveFile(
            string sourcePath,
            string dstFilePath)
        {
            Trace.TraceInformation(@"About to safe-move file from '{0}' to '{1}'.", sourcePath, dstFilePath);

            if (sourcePath == null || dstFilePath == null)
            {
                Trace.TraceInformation(
                    string.Format(
                        @"Source file path or destination file path does not exist. " +
                        @"Not moving."
                        ));
            }
            else
            {
                if (SafeFileExists(sourcePath))
                {
                    SafeDeleteFile(dstFilePath);

                    var d = ZlpPathHelper.GetDirectoryPathNameFromFilePath(dstFilePath);

                    if (!ZlpIOHelper.DirectoryExists(d))
                    {
                        Trace.TraceInformation(@"Creating non-existing folder '{0}'.", d);
                        ZlpIOHelper.CreateDirectory(d);
                    }

                    ZlpIOHelper.MoveFile(sourcePath, dstFilePath);
                }
                else
                {
                    Trace.TraceInformation(@"Source file path to move does not exist: '{0}'.", sourcePath);
                }
            }
        }
예제 #13
0
 public void Delete()
 {
     ZlpIOHelper.DeleteFile(_path);
 }
예제 #14
0
 public void CopyTo(
     ZlpFileInfo destinationFilePath,
     bool overwriteExisting)
 {
     ZlpIOHelper.CopyFile(_path, destinationFilePath._path, overwriteExisting);
 }
예제 #15
0
 public ZlpDirectoryInfo[] GetDirectories(string pattern)
 {
     return(ZlpIOHelper.GetDirectories(FullName, pattern));
 }
예제 #16
0
 public void MoveTo(ZlpFileInfo destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath.FullName);
 }
예제 #17
0
 public void MoveTo(string destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath);
 }
예제 #18
0
 public ZlpDirectoryInfo[] GetDirectories(string pattern, SearchOption searchOption)
 {
     return(ZlpIOHelper.GetDirectories(FullName, pattern, searchOption));
 }
예제 #19
0
 public void AppendText(string text, Encoding encoding = null)
 {
     ZlpIOHelper.AppendText(_path, text, encoding);
 }
예제 #20
0
 public string[] ReadAllLines(Encoding encoding)
 {
     return(ZlpIOHelper.ReadAllLines(_path, encoding));
 }
예제 #21
0
 public void WriteAllText(string text, Encoding encoding = null)
 {
     ZlpIOHelper.WriteAllText(_path, text, encoding);
 }
예제 #22
0
 public ZlpDirectoryInfo[] GetDirectories(SearchOption searchOption)
 {
     return(ZlpIOHelper.GetDirectories(FullName, searchOption));
 }
예제 #23
0
 public string ReadAllText(Encoding encoding)
 {
     return(ZlpIOHelper.ReadAllText(_path, encoding));
 }
예제 #24
0
 public string ReadAllText()
 {
     return(ZlpIOHelper.ReadAllText(_path));
 }
예제 #25
0
 public void DeleteFileAfterReboot()
 {
     ZlpIOHelper.DeleteFileAfterReboot(_path);
 }
예제 #26
0
 public void WriteAllBytes(byte[] content)
 {
     ZlpIOHelper.WriteAllBytes(_path, content);
 }
예제 #27
0
 public ZlpDirectoryInfo[] GetDirectories()
 {
     return(ZlpIOHelper.GetDirectories(FullName));
 }
예제 #28
0
 public void MoveToRecycleBin()
 {
     ZlpIOHelper.MoveFileToRecycleBin(_path);
 }
예제 #29
0
 public string[] ReadAllLines()
 {
     return(ZlpIOHelper.ReadAllLines(_path));
 }
예제 #30
0
 public byte[] ReadAllBytes()
 {
     return(ZlpIOHelper.ReadAllBytes(_path));
 }