C# Class GSF.IO.FilePath

Contains File and Path manipulation methods.
Show file Open project: GridProtectionAlliance/gsf

Public Methods

Method Description
AddPathSuffix ( string filePath ) : string

Makes sure path is suffixed with standard Path.DirectorySeparatorChar.

ConnectToNetworkShare ( string sharename, string userName, string password, string domain ) : void

Connects to a network share with the specified user's credentials.

DisconnectFromNetworkShare ( string sharename ) : void

Disconnects the specified network share.

DisconnectFromNetworkShare ( string sharename, bool force ) : void

Disconnects the specified network share.

DropPathRoot ( string filePath ) : string

Remove any path root present in the path.

GetAbsolutePath ( string filePath ) : string

Gets the absolute file path for the specified file name or relative file path.

GetApplicationDataFolder ( ) : string

Gets the path to the folder where data related to the current application can be stored.

GetAvailableFreeSpace ( string pathName, long &freeSpace, long &totalSize ) : bool

Tries to get the free space values for a given path. This path can be a network share or a mount point.

GetCommonApplicationDataFolder ( ) : string

Gets the path to the folder where data related to the current application can be stored as well as shared among different users.

GetDirectoryName ( string filePath ) : string

Gets the directory information from the specified file path.

This differs from Path.GetDirectoryName in that it will see if last name in path is a directory and, if it exists, will properly treat that part of path as a directory. The .NET path function always assumes last entry is a file name if path is not suffixed with a slash. For example: Path.GetDirectoryName(@"C:\Music") will return "C:\", however, FilePath.GetDirectoryName(@"C:\Music") will return "C:\Music\", so long as Music directory exists.

GetExtension ( string filePath ) : string

Gets the extension from the specified file path.

GetFileLength ( string fileName ) : long

Gets the size of the specified file.

GetFileList ( string path ) : string[]

Gets a list of files under the specified path. Search wildcard pattern (c:\Data\*.dat) can be used for including only the files matching the pattern or path wildcard pattern (c:\Data\*\*.dat) to indicate the inclusion of files under all subdirectories in the list.

GetFileName ( string filePath ) : string

Gets the file name and extension from the specified file path.

GetFileNameWithoutExtension ( string filePath ) : string

Gets the file name without extension from the specified file path.

GetFilePatternRegularExpression ( string fileSpec ) : string

Gets a regular expression pattern that simulates wildcard matching for filenames (wildcards are defined as '*' or '?' characters).

GetLastDirectoryName ( string filePath ) : string

Gets the last directory name from a file path.

GetLastDirectoryName(string) would return sub2 from c:\windows\sub2\filename.ext.

GetUniqueFilePath ( string originalFilePath ) : string

Gets a unique file path for the given file by checking for name collisions and adding a sequence number to the end of the file name if there is a collision.

This method is designed to handle the case where the user wishes to create a file in a folder with a given name when there is a possibility that the name is already taken. Using this method, it is possible to create files with names in the following format:

  • File.ext
  • File (1).ext
  • File (2).ext
  • ...
This method uses a linear search to find a unique file name, so it is suitable for situations where there are a small number of collisions for each file name. This will detect and fill gaps that can occur when files are deleted (for instance, if "File (1).ext" were deleted from the list above).
GetUniqueFilePathWithBinarySearch ( string originalFilePath ) : string

Gets a unique file path for the given file by checking for name collisions and adding a sequence number to the end of the file name if there is a collision.

This method is designed to handle the case where the user wishes to create a file in a folder with a given name when there is a possibility that the name is already taken. Using this method, it is possible to create files with names in the following format:

  • File.ext
  • File (1).ext
  • File (2).ext
  • ...
This method uses a binary search to find a unique file name, so it is suitable for situations where a large number of files will be created with the same file name, and the next unique file name needs to be found relatively quickly. It will not always detect gaps in the sequence numbers that can occur when files are deleted (for instance, if "File (1).ext" were deleted from the list above).
GetValidFileName ( string fileName, char replaceWithCharacter = '_' ) : string

Gets a valid file name by replacing invalid file name characters with replaceWithCharacter.

This function expects a file name, not a file name with a path. To properly get a valid file path, where all directory names and the file name are validated, use the GetValidFilePath. Calling the GetValidFileName function will a full path will yield all directory separators replaced with the replaceWithCharacter.

GetValidFilePath ( string filePath, char replaceWithCharacter = '_' ) : string

Gets a valid file path by replacing invalid file or directory name characters with replaceWithCharacter.

InApplicationPath ( string filePath ) : bool

Determines if the specified filePath is contained with the current executable path.

IsFilePatternMatch ( string fileSpecs, string filePath, bool ignoreCase ) : bool

Determines whether the specified file name matches any of the given file specs (wildcards are defined as '*' or '?' characters).

The syntax for fileSpecs adheres to the following rules:

  • Either '\' or '/' (as defined by Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar) can match the other.
  • A single '\' or '/' at the beginning of the pattern matches any valid path root (such as "C:\" or "\\server\share").
  • A '?' matches a single character which would be valid in a file name (as defined by Path.GetInvalidFileNameChars).
  • A '*' matches any number of characters which would be valid in a file name.
  • A sequence of "**\" or "**/" matches any number of sequential directories.
  • Any other character matches itself.
IsValidFileName ( string filePath ) : bool

Determines if the specified file name and path is valid.

RemovePathSuffix ( string filePath ) : string

Makes sure path is not suffixed with Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar.

TrimFileName ( string filePath, int length ) : string

Returns a file name, for display purposes, of the specified length using "..." to indicate a longer name.

Minimum value for the length parameter is 12. 12 will be used for any value specified as less than 12.

TryGetReadLock ( string fileName ) : bool

Attempts to get read access on a file.

TryGetReadLockExclusive ( string fileName ) : bool

Attempts to get read access on a file.

TryGetWriteLock ( string fileName ) : bool

Attempts to get write access on a file.

ValidatePathName ( string filePath ) : void

Ensures the supplied path name is valid.

Throws argument exceptions if the is invalid.

WaitForReadLock ( string fileName ) : void

Waits for the default duration (5 seconds) for read access on a file.

WaitForReadLock ( string fileName, double secondsToWait ) : void

Waits for read access on a file for the specified number of seconds.

WaitForReadLockExclusive ( string fileName ) : void

Waits for the default duration (5 seconds) for read access on a file.

WaitForReadLockExclusive ( string fileName, double secondsToWait ) : void

Waits for read access on a file for the specified number of seconds.

WaitForWriteLock ( string fileName ) : void

Waits for the default duration (5 seconds) for write access on a file.

WaitForWriteLock ( string fileName, double secondsToWait ) : void

Waits for write access on a file for the specified number of seconds.

WaitTillExists ( string fileName ) : void

Waits for the default duration (5 seconds) for a file to exist.

WaitTillExists ( string fileName, double secondsToWait ) : void

Waits for a file to exist for the specified number of seconds.

Private Methods

Method Description
FilePath ( ) : System
GetDiskFreeSpaceEx ( string lpDirectoryName, ulong &lpFreeBytesAvailable, ulong &lpTotalNumberOfBytes, ulong &lpTotalNumberOfFreeBytes ) : bool
WNetAddConnection2 ( NETRESOURCE &lpNetResource, string lpPassword, string lpUsername, int dwFlags ) : int
WNetCancelConnection2 ( string lpName, int dwFlags, [ fForce ) : int

Method Details

AddPathSuffix() public static method

Makes sure path is suffixed with standard Path.DirectorySeparatorChar.
public static AddPathSuffix ( string filePath ) : string
filePath string The file path to be suffixed.
return string

ConnectToNetworkShare() public static method

Connects to a network share with the specified user's credentials.
public static ConnectToNetworkShare ( string sharename, string userName, string password, string domain ) : void
sharename string UNC share name to connect to.
userName string User name to use for connection.
password string Password to use for connection.
domain string Domain name to use for connection. Specify the computer name for local system accounts.
return void

DisconnectFromNetworkShare() public static method

Disconnects the specified network share.
public static DisconnectFromNetworkShare ( string sharename ) : void
sharename string UNC share name to disconnect from.
return void

DisconnectFromNetworkShare() public static method

Disconnects the specified network share.
public static DisconnectFromNetworkShare ( string sharename, bool force ) : void
sharename string UNC share name to disconnect from.
force bool true to force a disconnect; otherwise false.
return void

DropPathRoot() public static method

Remove any path root present in the path.
public static DropPathRoot ( string filePath ) : string
filePath string The file path whose root is to be removed.
return string

GetAbsolutePath() public static method

Gets the absolute file path for the specified file name or relative file path.
public static GetAbsolutePath ( string filePath ) : string
filePath string File name or relative file path.
return string

GetApplicationDataFolder() public static method

Gets the path to the folder where data related to the current application can be stored.
public static GetApplicationDataFolder ( ) : string
return string

GetAvailableFreeSpace() public static method

Tries to get the free space values for a given path. This path can be a network share or a mount point.
public static GetAvailableFreeSpace ( string pathName, long &freeSpace, long &totalSize ) : bool
pathName string The path to the location
freeSpace long The number of user space bytes
totalSize long The total number of bytes on the drive.
return bool

GetCommonApplicationDataFolder() public static method

Gets the path to the folder where data related to the current application can be stored as well as shared among different users.
public static GetCommonApplicationDataFolder ( ) : string
return string

GetDirectoryName() public static method

Gets the directory information from the specified file path.
This differs from Path.GetDirectoryName in that it will see if last name in path is a directory and, if it exists, will properly treat that part of path as a directory. The .NET path function always assumes last entry is a file name if path is not suffixed with a slash. For example: Path.GetDirectoryName(@"C:\Music") will return "C:\", however, FilePath.GetDirectoryName(@"C:\Music") will return "C:\Music\", so long as Music directory exists.
public static GetDirectoryName ( string filePath ) : string
filePath string The file path from which the directory information is to be obtained.
return string

GetExtension() public static method

Gets the extension from the specified file path.
public static GetExtension ( string filePath ) : string
filePath string The file path from which the extension is to be obtained.
return string

GetFileLength() public static method

Gets the size of the specified file.
public static GetFileLength ( string fileName ) : long
fileName string Name of file whose size is to be retrieved.
return long

GetFileList() public static method

Gets a list of files under the specified path. Search wildcard pattern (c:\Data\*.dat) can be used for including only the files matching the pattern or path wildcard pattern (c:\Data\*\*.dat) to indicate the inclusion of files under all subdirectories in the list.
public static GetFileList ( string path ) : string[]
path string The path for which a list of files is to be returned.
return string[]

GetFileName() public static method

Gets the file name and extension from the specified file path.
public static GetFileName ( string filePath ) : string
filePath string The file path from which the file name and extension is to be obtained.
return string

GetFileNameWithoutExtension() public static method

Gets the file name without extension from the specified file path.
public static GetFileNameWithoutExtension ( string filePath ) : string
filePath string The file path from which the file name is to be obtained.
return string

GetFilePatternRegularExpression() public static method

Gets a regular expression pattern that simulates wildcard matching for filenames (wildcards are defined as '*' or '?' characters).
public static GetFilePatternRegularExpression ( string fileSpec ) : string
fileSpec string The file spec for which the regular expression pattern if to be generated.
return string

GetLastDirectoryName() public static method

Gets the last directory name from a file path.
GetLastDirectoryName(string) would return sub2 from c:\windows\sub2\filename.ext.
public static GetLastDirectoryName ( string filePath ) : string
filePath string The file path from where the last directory name is to be retrieved.
return string

GetUniqueFilePath() public static method

Gets a unique file path for the given file by checking for name collisions and adding a sequence number to the end of the file name if there is a collision.
This method is designed to handle the case where the user wishes to create a file in a folder with a given name when there is a possibility that the name is already taken. Using this method, it is possible to create files with names in the following format:
  • File.ext
  • File (1).ext
  • File (2).ext
  • ...
This method uses a linear search to find a unique file name, so it is suitable for situations where there are a small number of collisions for each file name. This will detect and fill gaps that can occur when files are deleted (for instance, if "File (1).ext" were deleted from the list above).
public static GetUniqueFilePath ( string originalFilePath ) : string
originalFilePath string The path to the original file before adding the sequence number.
return string

GetUniqueFilePathWithBinarySearch() public static method

Gets a unique file path for the given file by checking for name collisions and adding a sequence number to the end of the file name if there is a collision.
This method is designed to handle the case where the user wishes to create a file in a folder with a given name when there is a possibility that the name is already taken. Using this method, it is possible to create files with names in the following format:
  • File.ext
  • File (1).ext
  • File (2).ext
  • ...
This method uses a binary search to find a unique file name, so it is suitable for situations where a large number of files will be created with the same file name, and the next unique file name needs to be found relatively quickly. It will not always detect gaps in the sequence numbers that can occur when files are deleted (for instance, if "File (1).ext" were deleted from the list above).
public static GetUniqueFilePathWithBinarySearch ( string originalFilePath ) : string
originalFilePath string The path to the original file before adding the sequence number.
return string

GetValidFileName() public static method

Gets a valid file name by replacing invalid file name characters with replaceWithCharacter.
This function expects a file name, not a file name with a path. To properly get a valid file path, where all directory names and the file name are validated, use the GetValidFilePath. Calling the GetValidFileName function will a full path will yield all directory separators replaced with the replaceWithCharacter.
public static GetValidFileName ( string fileName, char replaceWithCharacter = '_' ) : string
fileName string File name to validate.
replaceWithCharacter char Character to replace invalid file name characters with. Set to '\0' to remove invalid file name characters.
return string

GetValidFilePath() public static method

Gets a valid file path by replacing invalid file or directory name characters with replaceWithCharacter.
public static GetValidFilePath ( string filePath, char replaceWithCharacter = '_' ) : string
filePath string File path to validate.
replaceWithCharacter char Character to replace invalid file or directory name characters with. Set to '\0' to remove invalid file or directory name characters.
return string

InApplicationPath() public static method

Determines if the specified filePath is contained with the current executable path.
public static InApplicationPath ( string filePath ) : bool
filePath string File name or relative file path.
return bool

IsFilePatternMatch() public static method

Determines whether the specified file name matches any of the given file specs (wildcards are defined as '*' or '?' characters).
The syntax for fileSpecs adheres to the following rules:
  • Either '\' or '/' (as defined by Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar) can match the other.
  • A single '\' or '/' at the beginning of the pattern matches any valid path root (such as "C:\" or "\\server\share").
  • A '?' matches a single character which would be valid in a file name (as defined by Path.GetInvalidFileNameChars).
  • A '*' matches any number of characters which would be valid in a file name.
  • A sequence of "**\" or "**/" matches any number of sequential directories.
  • Any other character matches itself.
contains one or more of the invalid characters defined in .
public static IsFilePatternMatch ( string fileSpecs, string filePath, bool ignoreCase ) : bool
fileSpecs string The file specs used for matching the specified file name.
filePath string The file path to be tested against the specified file specs for a match.
ignoreCase bool true to specify a case-insensitive match; otherwise false.
return bool

IsValidFileName() public static method

Determines if the specified file name and path is valid.
public static IsValidFileName ( string filePath ) : bool
filePath string The file name with optional path to test for validity.
return bool

RemovePathSuffix() public static method

Makes sure path is not suffixed with Path.DirectorySeparatorChar or Path.AltDirectorySeparatorChar.
public static RemovePathSuffix ( string filePath ) : string
filePath string The file path to be unsuffixed.
return string

TrimFileName() public static method

Returns a file name, for display purposes, of the specified length using "..." to indicate a longer name.
Minimum value for the length parameter is 12. 12 will be used for any value specified as less than 12.
public static TrimFileName ( string filePath, int length ) : string
filePath string The file path to be trimmed.
length int The maximum length of the trimmed file path.
return string

TryGetReadLock() public static method

Attempts to get read access on a file.
public static TryGetReadLock ( string fileName ) : bool
fileName string The file to check for read access.
return bool

TryGetReadLockExclusive() public static method

Attempts to get read access on a file.
public static TryGetReadLockExclusive ( string fileName ) : bool
fileName string The file to check for read access.
return bool

TryGetWriteLock() public static method

Attempts to get write access on a file.
public static TryGetWriteLock ( string fileName ) : bool
fileName string The file to check for write access.
return bool

ValidatePathName() public static method

Ensures the supplied path name is valid.
Throws argument exceptions if the is invalid.
public static ValidatePathName ( string filePath ) : void
filePath string The file path to be validated.
return void

WaitForReadLock() public static method

Waits for the default duration (5 seconds) for read access on a file.
public static WaitForReadLock ( string fileName ) : void
fileName string The name of the file to wait for to obtain read access.
return void

WaitForReadLock() public static method

Waits for read access on a file for the specified number of seconds.
public static WaitForReadLock ( string fileName, double secondsToWait ) : void
fileName string The name of the file to wait for to obtain read access.
secondsToWait double The time to wait for in seconds to obtain read access on a file. Set to zero to wait infinitely.
return void

WaitForReadLockExclusive() public static method

Waits for the default duration (5 seconds) for read access on a file.
public static WaitForReadLockExclusive ( string fileName ) : void
fileName string The name of the file to wait for to obtain read access.
return void

WaitForReadLockExclusive() public static method

Waits for read access on a file for the specified number of seconds.
public static WaitForReadLockExclusive ( string fileName, double secondsToWait ) : void
fileName string The name of the file to wait for to obtain read access.
secondsToWait double The time to wait for in seconds to obtain read access on a file. Set to zero to wait infinitely.
return void

WaitForWriteLock() public static method

Waits for the default duration (5 seconds) for write access on a file.
public static WaitForWriteLock ( string fileName ) : void
fileName string The name of the file to wait for to obtain write access.
return void

WaitForWriteLock() public static method

Waits for write access on a file for the specified number of seconds.
public static WaitForWriteLock ( string fileName, double secondsToWait ) : void
fileName string The name of the file to wait for to obtain write access.
secondsToWait double The time to wait for in seconds to obtain write access on a file. Set to zero to wait infinitely.
return void

WaitTillExists() public static method

Waits for the default duration (5 seconds) for a file to exist.
public static WaitTillExists ( string fileName ) : void
fileName string The name of the file to wait for until it is created.
return void

WaitTillExists() public static method

Waits for a file to exist for the specified number of seconds.
public static WaitTillExists ( string fileName, double secondsToWait ) : void
fileName string The name of the file to wait for until it is created.
secondsToWait double The time to wait for in seconds for the file to be created. Set to zero to wait infinitely.
return void