/// <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)); }
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); } }
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()); } }
public ZlpDirectoryInfo CreateSubdirectory(string name) { var path = ZlpPathHelper.Combine(FullName, name); ZlpIOHelper.CreateDirectory(path); return(new ZlpDirectoryInfo(path)); }
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); } }
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)); }
public System.IO.FileStream OpenRead() { return(new System.IO.FileStream( ZlpIOHelper.CreateFileHandle( FullName, CreationDisposition.OpenAlways, FileAccess.GenericRead, FileShare.Read), System.IO.FileAccess.Read)); }
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); } } } }
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); } } }
public void Delete() { ZlpIOHelper.DeleteFile(_path); }
public void CopyTo( ZlpFileInfo destinationFilePath, bool overwriteExisting) { ZlpIOHelper.CopyFile(_path, destinationFilePath._path, overwriteExisting); }
public ZlpDirectoryInfo[] GetDirectories(string pattern) { return(ZlpIOHelper.GetDirectories(FullName, pattern)); }
public void MoveTo(ZlpFileInfo destinationFilePath) { ZlpIOHelper.MoveFile(_path, destinationFilePath.FullName); }
public void MoveTo(string destinationFilePath) { ZlpIOHelper.MoveFile(_path, destinationFilePath); }
public ZlpDirectoryInfo[] GetDirectories(string pattern, SearchOption searchOption) { return(ZlpIOHelper.GetDirectories(FullName, pattern, searchOption)); }
public void AppendText(string text, Encoding encoding = null) { ZlpIOHelper.AppendText(_path, text, encoding); }
public string[] ReadAllLines(Encoding encoding) { return(ZlpIOHelper.ReadAllLines(_path, encoding)); }
public void WriteAllText(string text, Encoding encoding = null) { ZlpIOHelper.WriteAllText(_path, text, encoding); }
public ZlpDirectoryInfo[] GetDirectories(SearchOption searchOption) { return(ZlpIOHelper.GetDirectories(FullName, searchOption)); }
public string ReadAllText(Encoding encoding) { return(ZlpIOHelper.ReadAllText(_path, encoding)); }
public string ReadAllText() { return(ZlpIOHelper.ReadAllText(_path)); }
public void DeleteFileAfterReboot() { ZlpIOHelper.DeleteFileAfterReboot(_path); }
public void WriteAllBytes(byte[] content) { ZlpIOHelper.WriteAllBytes(_path, content); }
public ZlpDirectoryInfo[] GetDirectories() { return(ZlpIOHelper.GetDirectories(FullName)); }
public void MoveToRecycleBin() { ZlpIOHelper.MoveFileToRecycleBin(_path); }
public string[] ReadAllLines() { return(ZlpIOHelper.ReadAllLines(_path)); }
public byte[] ReadAllBytes() { return(ZlpIOHelper.ReadAllBytes(_path)); }