/// <summary> /// Retrieves the path part in a given string (without the drive or share). /// </summary> /// <remarks> /// Example: "C:\Team\Text\Test.Txt" would return "\Test\Text\". /// - /// Please note_: Searches until the last backslash (including). /// If not present, the path is not treated as a directory. /// (E.g.. "C:\Test\MyDir" would return "\Test" only as the directory). /// </remarks> public static string GetDirectory( string path) { if (string.IsNullOrEmpty(path)) { return(path); } else { var driveOrShare = GetDriveOrShare(path); var dir = ZlpPathHelper.GetDirectoryPathNameFromFilePath(path); // ReSharper disable InvocationIsSkipped Debug.Assert( string.IsNullOrEmpty(driveOrShare) || dir.StartsWith(driveOrShare), string.Format( @"Variable 'dir' ('{0}') must start with drive or share '{1}'.", dir, driveOrShare)); // ReSharper restore InvocationIsSkipped if (!string.IsNullOrEmpty(driveOrShare) && dir.StartsWith(driveOrShare)) { return(dir.Substring(driveOrShare.Length)); } else { return(dir); } } }
/// <summary> /// Gets the drive or share and directory. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> public static string GetDriveOrShareAndDirectory( string path) { return(string.IsNullOrEmpty(path) ? path : ZlpPathHelper.GetDirectoryPathNameFromFilePath(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 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); } } }