C# Класс LazyCopy.Utilities.PathHelper

Contains helper functions to work with file system.
Показать файл Открыть проект

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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\).

Описание методов

ChangeDeviceNameToDriveLetter() публичный статический Метод

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.
Результат string

ChangeDriveLetterToDeviceName() публичный статический Метод

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.
Результат string

ChangeParent() публичный статический Метод

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.
Результат string

CompactPath() публичный статический Метод

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.
Результат string

EndWithDirectorySeparator() публичный статический Метод

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.
Результат string

GenerateUniqueFileName() публичный статический Метод

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 .
Результат string

GetFileNameWithoutExtension() публичный статический Метод

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.
Результат string