Class PathFormatter
Helper for formatting file paths. It takes care of maximum path length and of creating the path, which does not yet exists on disk.
Inheritance
Namespace: Kros.IO
Assembly: Kros.Utils.dll
Syntax
public class PathFormatter
Remarks
The class is useful for example in exports, when there are multiple files generated by export. The user of the application sets just output folder and file names are generated automatically based on exported data. The class ensures, that the generated paths are valid (do not contain invalid characters) and that they are not too long. It also checks the existence of files. If there already exists a file, counter is added to generated path.
It is even possible to add own string as an additional information to generated paths.
Static property Default is created for simple direct use of PathFormatter
.
Properties
CounterClosingString
Closing string for counter, which is inserted into file/folder name.
The counter is enclosed between CounterOpeningString and CounterClosingString.
Default value is right parenthesis )
.
Declaration
public string CounterClosingString { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
When set, invalid path characters in the value are replaced using ReplaceInvalidPathChars(String).
CounterOpeningString
Opening string for counter, which is inserted into file/folder name.
The counter is enclosed between CounterOpeningString and CounterClosingString.
Default value is left parenthesis (
.
Declaration
public string CounterOpeningString { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
When set, invalid path characters in the value are replaced using ReplaceInvalidPathChars(String).
Default
Default instance for simpler direct use.
Declaration
public static PathFormatter Default { get; }
Property Value
Type | Description |
---|---|
PathFormatter |
InfoClosingString
Closing string for additional information, which is inserted into file/folder name.
Additional information is enclosed between InfoOpeningString and InfoClosingString.
Default value is right parenthesis )
.
Declaration
public string InfoClosingString { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
When set, invalid path characters in the value are replaced using ReplaceInvalidPathChars(String).
InfoOpeningString
Opening string for additional information, which is inserted into file/folder name.
Additional information is enclosed between InfoOpeningString and InfoClosingString.
Default value is left parenthesis (
.
Declaration
public string InfoOpeningString { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
When set, invalid path characters in the value are replaced using ReplaceInvalidPathChars(String).
MaxPathLength
Maximum path length. Intended for internal use.
Declaration
protected int MaxPathLength { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
FileExists(String)
Checks if file filePath
exists. Method is intended for internal use.
Declaration
public virtual bool FileExists(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | Path to file. |
Returns
Type | Description |
---|---|
System.Boolean | true if file exists, false otherwise. |
FolderExists(String)
Checks if folder folderPath
exists. Method is intended for internal use.
Declaration
public virtual bool FolderExists(string folderPath)
Parameters
Type | Name | Description |
---|---|---|
System.String | folderPath | Path to folder. |
Returns
Type | Description |
---|---|
System.Boolean | true if folder exists, false otherwise. |
FormatDateForPath(DateTime)
Formats date for use in file/folder names. Default format is yyyy_MM_dd
.
Declaration
protected virtual string FormatDateForPath(DateTime value)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | Date to format. |
Returns
Type | Description |
---|---|
System.String |
Remarks
Minimum (System.DateTime.MinValue) and maximum (System.DateTime.MaxValue) values are not formatted, but empty string is returned.
FormatNewPath(String, String)
Formats folder folder
and file name fileName
to the resulting path and
ensures, that the file does not exist. The counter is added to file name if the path exists.
Declaration
public string FormatNewPath(string folder, string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | folder | Folder path. |
System.String | fileName | File name. |
Returns
Type | Description |
---|---|
System.String |
Remarks
If file already exists at generated path, such counter is added to the file name ((1)
, (2)
...),
to make the path to non-existing file. If resulting path is too long, file name is shortened to make it valid.
Shortened is original fileName
- if counter was added, it is preserved.
FormatNewPath(String, String, String)
Formats folder folder
and file name fileName
to the resulting path.
Additional info info
is added to the file name and it is ensured, that the file does not exist.
The counter is added to file name if the path exists.
Declaration
public string FormatNewPath(string folder, string fileName, string info)
Parameters
Type | Name | Description |
---|---|---|
System.String | folder | Folder path. |
System.String | fileName | File name. |
System.String | info | Additional info added to the file name. |
Returns
Type | Description |
---|---|
System.String |
Remarks
Additional info info
is added to the file name enclosed between InfoOpeningString
and InfoClosingString. So if fileName
is exported.xml
and
info
is Lorem ipsum
, resulting file name is exported (Lorem ipsum).xml
.
If file already exists at generated path, such counter is added to the file name ((1)
, (2)
...),
to make the path to non-existing false, so final file name may be
exported (Lorem ipsum) (1).xml
.
If resulting path is too long, the file name is shortened to make the path correct. Shortened is the original
fileName
, additional info info
and possible counter are preserved.
FormatPath(String, String)
Formats folder folder
and file name fileName
to the resulting path.
It does not check if the file already exist.
Declaration
public string FormatPath(string folder, string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | folder | Folder path. |
System.String | fileName | File name. |
Returns
Type | Description |
---|---|
System.String |
Remarks
If resulting path is too long, the file name is shortened to make the path correct.
FormatPath(String, String, String)
Formats folder folder
and file name fileName
to the resulting path.
Additional info info
is added to the file name. It does not check if the file already exist.
Declaration
public string FormatPath(string folder, string fileName, string info)
Parameters
Type | Name | Description |
---|---|---|
System.String | folder | Folder path. |
System.String | fileName | File name. |
System.String | info | Additional info added to the file name. |
Returns
Type | Description |
---|---|
System.String |
Remarks
Additional info info
is added to the file name enclosed between InfoOpeningString
and InfoClosingString. So if fileName
is exported.xml
and
info
is Lorem ipsum
, resulting file name is exported (Lorem ipsum).xml
.
If resulting path is too long, the file name is shortened to make the path correct. Shortened is the original
fileName
, additional info info
is preserved.
FormatPaths<TKey>(String, String, Dictionary<TKey, String>)
Creates a list of paths to files. The paths are created with base folder baseFolder
,
base file name baseFileName
and additional information for individual files
fileInfos
.
Declaration
public Dictionary<TKey, string> FormatPaths<TKey>(string baseFolder, string baseFileName, Dictionary<TKey, string> fileInfos)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseFolder | Base folder for generated file paths. |
System.String | baseFileName | Base file name. All paths are created with this file name with additional info added. |
System.Collections.Generic.Dictionary<TKey, System.String> | fileInfos | Additional informations added to individual file names. |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, System.String> | Dictionary with the same keys as int |
Type Parameters
Name | Description |
---|---|
TKey | Type of the key in additional info dictionary |
Remarks
For every key and value in fileInfos
si generated a file path in resultng dictionary.
File paths are created using FormatPath(String, String, String), meaning that file name is
created as baseFileName
with additional information added from fileInfos
and joined with baseFolder
.
If needed, resulting paths are shortened to be valid. All of the resulting paths are shortened the same way
and in such a way that the longest of them must be valid. Base file name baseFileName
is
shortened, additional informations are preserved.
Resulting paths are checked if they already exists, and if so, a counter is added to the filename. So returned generated paths do not exist on file system (at least at the time of their generation).
Examples
If baseFolder
is C:\lorem\ipsum
, baseFileName
is filename.xml
and values in fileInfos
are:
key | additional file name info |
---|---|
1 | some info 1 |
2 | some info 2 |
3 | some info 3 |
Created list of paths is:
key | path |
---|---|
1 | C:\lorem\ipsum\filename (some info 1).xml |
2 | C:\lorem\ipsum\filename (some info 2).xml |
3 | C:\lorem\ipsum\filename (some info 3).xml |
FormatPathsInSubfolder<TKey>(String, String, Dictionary<TKey, String>)
Creates a list of paths to files. The paths are created with base folder baseFolder
,
to which a subfolder is added with the same name as value of baseFileName
without extension.
File names are created as value of baseFileName
and additional information from
fileInfos
.
Declaration
public Dictionary<TKey, string> FormatPathsInSubfolder<TKey>(string baseFolder, string baseFileName, Dictionary<TKey, string> fileInfos)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseFolder | Base folder for generated file paths. Another subfolder is added with the same name
as the file name |
System.String | baseFileName | Base file name. All paths are created with this file name with additional info added.
At the same time this value (without the extension) is used as a subfolder name in |
System.Collections.Generic.Dictionary<TKey, System.String> | fileInfos | Additional informations added to individual file names. |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, System.String> | Dictionary with the same keys as int |
Type Parameters
Name | Description |
---|---|
TKey | Type of the key in additional info dictionary |
Remarks
For every key and value in fileInfos
si generated a file path in resultng dictionary.
At first, base path baseFolder
is extended with subfolder with name as file name in
baseFileName
(without extension). File paths are then created using
FormatPath(String, String, String), meaning that file name is created as
baseFileName
with additional information added from fileInfos
.
If needed, resulting paths are shortened to be valid. All of the resulting paths are shortened the same way
and in such a way that the longest of them must be valid. The value of base file name baseFileName
is shortened before it is added as a subfolder to baseFolder
. So when shortening occurs,
the subfolder and file names are shortened. Additional informations are preserved.
Resulting paths are checked if they already exists, and if so, a counter is added to the filename. So returned generated paths do not exist on file system (at least at the time of their generation).
Examples
If baseFolder
is C:\lorem\ipsum
, baseFileName
is filename.xml
and values in fileInfos
are:
key | additional file name info |
---|---|
1 | some info 1 |
2 | some info 2 |
3 | some info 3 |
Created list of paths is:
key | path |
---|---|
1 | C:\lorem\ipsum\filename\filename (some info 1).xml |
2 | C:\lorem\ipsum\filename\filename (some info 2).xml |
3 | C:\lorem\ipsum\filename\filename (some info 3).xml |
FormatPathsInSubfolder<TKey>(String, String, String, Dictionary<TKey, String>)
Creates a list of paths to files. The paths are created with base folder baseFolder
,
to which a subfolder is added. Subfolder name is the same name as value of baseFileName
without extension and subfolderInfo
is added to it. File names are created as value of
baseFileName
and additional information from fileInfos
.
Declaration
public Dictionary<TKey, string> FormatPathsInSubfolder<TKey>(string baseFolder, string baseFileName, string subfolderInfo, Dictionary<TKey, string> fileInfos)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseFolder | Base folder for generated file paths. Another subfolder is added with the same name
as the file name |
System.String | baseFileName | Base file name. All paths are created with this file name with additional info added.
At the same time this value (without the extension) is used as a subfolder name in |
System.String | subfolderInfo | Additional info added to subfolder name. |
System.Collections.Generic.Dictionary<TKey, System.String> | fileInfos | Additional informations added to individual file names. |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, System.String> | Dictionary with the same keys as in |
Type Parameters
Name | Description |
---|---|
TKey | Type of the key in additional info dictionary |
Remarks
For every key and value in fileInfos
si generated a file path in resultng dictionary.
At first, base path baseFolder
is extended with subfolder with name as file name in
baseFileName
(without extension) and subfolderInfo
.
File paths are then created using FormatPath(String, String, String), meaning that file name is
created as baseFileName
with additional information added from fileInfos
.
If needed, resulting paths are shortened to be valid. All of the resulting paths are shortened the same way
and in such a way that the longest of them must be valid. The value of base file name baseFileName
is shortened before it is added as a subfolder to baseFolder
. So when shortening occurs,
the subfolder and file names are shortened. Additional informations fileInfos
and
subfolderInfo
are preserved.
Resulting paths are checked if they already exists, and if so, a counter is added to the filename. So returned generated paths do not exist on file system (at least at the time of their generation).
Examples
If baseFolder
is C:\lorem\ipsum
, baseFileName
is filename.xml
, subfolderInfo
is subfolder info
and values in
fileInfos
are:
key | additional file name info |
---|---|
1 | some info 1 |
2 | some info 2 |
3 | some info 3 |
Created list of paths is:
key | path |
---|---|
1 | C:\lorem\ipsum\filename (subfolder info)\filename (some info 1).xml |
2 | C:\lorem\ipsum\filename (subfolder info)\filename (some info 2).xml |
3 | C:\lorem\ipsum\filename (subfolder info)\filename (some info 3).xml |
FormatSeasonForPath(DateTime, DateTime)
Formats season from
- to
for use in file/folder names.
Declaration
public virtual string FormatSeasonForPath(DateTime from, DateTime to)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | from | Start of the season. |
System.DateTime | to | End of the season. |
Returns
Type | Description |
---|---|
System.String |
Remarks
If any value is minimum or maximum date (System.DateTime.MinValue, System.DateTime.MaxValue), the value is not used. For the formatting itself is used the method FormatDateForPath(DateTime).