C# Class LazyCopy.Utilities.PathHelper

Contains helper functions to work with file system.
Afficher le fichier Open project: aleksk/LazyCopy

Méthodes publiques

Méthode Description
ChangeDeviceNameToDriveLetter ( string path ) : string

Changes the DOS device name for the path given to the according drive letter.

This method also supports UNC paths.

ChangeDriveLetterToDeviceName ( string path ) : string

Changes the drive letter for the path given to the according DOS device name.

This method also supports UNC paths.

ChangeParent ( string path, string oldParent, string newParent ) : string

Changes the parent of a path string.

This method is useful, when you need not just replace the path root, but one of its parent.
For example, to transform C:\Source\SubDir\File.txt into D:\Target\SubDir\File.txt the following code may be used: string targetFile = PathHelper.ChangeParent(@"C:\Source\File.txt", @"C:\Source", @"D:\Target");

CompactPath ( string longPathName, int newLength ) : string

Truncates a path to fit within a certain number of characters by replacing path components with ellipses.

The '/' separator will be used instead of '\' if the original string used it.
If longPathName points to a file name that is too long, instead of a path, the file name will be truncated to newLength characters, including the ellipsis and the terminating character.
For example, if the input file name is "My Filename" and newLength is 10, this method will return "My Fil...".

EndWithDirectorySeparator ( string path ) : string

Makes sure that the path ends with the directory separator.

GenerateUniqueFileName ( string parentDirectory, string prefix, string extension ) : string

Generates unique file name in the parentDirectory.

The result file name will look like the following: <prefix><GUID>.<extension>
For example:
PathHelper.GenerateUniqueFileName(@"C:\Temp\", "a_", "bin"); // C:\Temp\a_8F3C680522E0457896837ADF38ED8E6A.bin PathHelper.GenerateUniqueFileName(@"C:\Temp\", "b_", ".txt"); // C:\Temp\b_BF3C086BE0C444B4906E4A36CBC16551.txt

GetFileNameWithoutExtension ( string path, string extension ) : string

Returns the file name of the specified path string without the extension.

If the path does not end with the extension, its original value is returned.

Private Methods

Méthode Description
GetDeviceDosName ( string rootPath ) : string

Queries OS for the device name.

UpdateDeviceMappings ( ) : void

Updates the internal maps between the volume names (D:\) and device names (\Device\HarddiskVolume2\).

Method Details

ChangeDeviceNameToDriveLetter() public static méthode

Changes the DOS device name for the path given to the according drive letter.
This method also supports UNC paths.
is or empty. does not start with a DOS device name. Drive letter could not be found for the given.
public static ChangeDeviceNameToDriveLetter ( string path ) : string
path string Path to replace DOS device name for.
Résultat string

ChangeDriveLetterToDeviceName() public static méthode

Changes the drive letter for the path given to the according DOS device name.
This method also supports UNC paths.
is or empty. is too short. The according DOS device name could not be found.
public static ChangeDriveLetterToDeviceName ( string path ) : string
path string Path to replace the drive letter for.
Résultat string

ChangeParent() public static méthode

Changes the parent of a path string.
This method is useful, when you need not just replace the path root, but one of its parent.
For example, to transform C:\Source\SubDir\File.txt into D:\Target\SubDir\File.txt the following code may be used: string targetFile = PathHelper.ChangeParent(@"C:\Source\File.txt", @"C:\Source", @"D:\Target");
Any of the parameters is or empty. string does not start with the .
public static ChangeParent ( string path, string oldParent, string newParent ) : string
path string The path information to modify.
oldParent string The old parent.
newParent string The new parent.
Résultat string

CompactPath() public static méthode

Truncates a path to fit within a certain number of characters by replacing path components with ellipses.
The '/' separator will be used instead of '\' if the original string used it.
If longPathName points to a file name that is too long, instead of a path, the file name will be truncated to newLength characters, including the ellipsis and the terminating character.
For example, if the input file name is "My Filename" and newLength is 10, this method will return "My Fil...".
is or empty. is negative or equal to zero. cannot be compacted.
public static CompactPath ( string longPathName, int newLength ) : string
longPathName string Path to be altered.
newLength int The maximum number of characters to be contained in the new string.
Résultat string

EndWithDirectorySeparator() public static méthode

Makes sure that the path ends with the directory separator.
is or empty.
public static EndWithDirectorySeparator ( string path ) : string
path string The path information to modify.
Résultat string

GenerateUniqueFileName() public static méthode

Generates unique file name in the parentDirectory.
The result file name will look like the following: <prefix><GUID>.<extension>
For example:
PathHelper.GenerateUniqueFileName(@"C:\Temp\", "a_", "bin"); // C:\Temp\a_8F3C680522E0457896837ADF38ED8E6A.bin PathHelper.GenerateUniqueFileName(@"C:\Temp\", "b_", ".txt"); // C:\Temp\b_BF3C086BE0C444B4906E4A36CBC16551.txt
is .
public static GenerateUniqueFileName ( string parentDirectory, string prefix, string extension ) : string
parentDirectory string Directory, where the file will be stored into.
prefix string File prefix. May be .
extension string File extension. May be .
Résultat string

GetFileNameWithoutExtension() public static méthode

Returns the file name of the specified path string without the extension.
If the path does not end with the extension, its original value is returned.
or is or empty.
public static GetFileNameWithoutExtension ( string path, string extension ) : string
path string Path to the file.
extension string Extension to be removed (with or without leading dot), if the has it.
Résultat string