Exemplo n.º 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));
 }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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());
            }
        }
Exemplo n.º 5
0
        public ZlpDirectoryInfo CreateSubdirectory(string name)
        {
            var path = ZlpPathHelper.Combine(FullName, name);

            ZlpIOHelper.CreateDirectory(path);
            return(new ZlpDirectoryInfo(path));
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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));
 }
Exemplo n.º 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));
 }
Exemplo n.º 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);
                    }
                }
            }
        }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 13
0
 public void Delete()
 {
     ZlpIOHelper.DeleteFile(_path);
 }
Exemplo n.º 14
0
 public void CopyTo(
     ZlpFileInfo destinationFilePath,
     bool overwriteExisting)
 {
     ZlpIOHelper.CopyFile(_path, destinationFilePath._path, overwriteExisting);
 }
Exemplo n.º 15
0
 public ZlpDirectoryInfo[] GetDirectories(string pattern)
 {
     return(ZlpIOHelper.GetDirectories(FullName, pattern));
 }
Exemplo n.º 16
0
 public void MoveTo(ZlpFileInfo destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath.FullName);
 }
Exemplo n.º 17
0
 public void MoveTo(string destinationFilePath)
 {
     ZlpIOHelper.MoveFile(_path, destinationFilePath);
 }
Exemplo n.º 18
0
 public ZlpDirectoryInfo[] GetDirectories(string pattern, SearchOption searchOption)
 {
     return(ZlpIOHelper.GetDirectories(FullName, pattern, searchOption));
 }
Exemplo n.º 19
0
 public void AppendText(string text, Encoding encoding = null)
 {
     ZlpIOHelper.AppendText(_path, text, encoding);
 }
Exemplo n.º 20
0
 public string[] ReadAllLines(Encoding encoding)
 {
     return(ZlpIOHelper.ReadAllLines(_path, encoding));
 }
Exemplo n.º 21
0
 public void WriteAllText(string text, Encoding encoding = null)
 {
     ZlpIOHelper.WriteAllText(_path, text, encoding);
 }
Exemplo n.º 22
0
 public ZlpDirectoryInfo[] GetDirectories(SearchOption searchOption)
 {
     return(ZlpIOHelper.GetDirectories(FullName, searchOption));
 }
Exemplo n.º 23
0
 public string ReadAllText(Encoding encoding)
 {
     return(ZlpIOHelper.ReadAllText(_path, encoding));
 }
Exemplo n.º 24
0
 public string ReadAllText()
 {
     return(ZlpIOHelper.ReadAllText(_path));
 }
Exemplo n.º 25
0
 public void DeleteFileAfterReboot()
 {
     ZlpIOHelper.DeleteFileAfterReboot(_path);
 }
Exemplo n.º 26
0
 public void WriteAllBytes(byte[] content)
 {
     ZlpIOHelper.WriteAllBytes(_path, content);
 }
Exemplo n.º 27
0
 public ZlpDirectoryInfo[] GetDirectories()
 {
     return(ZlpIOHelper.GetDirectories(FullName));
 }
Exemplo n.º 28
0
 public void MoveToRecycleBin()
 {
     ZlpIOHelper.MoveFileToRecycleBin(_path);
 }
Exemplo n.º 29
0
 public string[] ReadAllLines()
 {
     return(ZlpIOHelper.ReadAllLines(_path));
 }
Exemplo n.º 30
0
 public byte[] ReadAllBytes()
 {
     return(ZlpIOHelper.ReadAllBytes(_path));
 }