This utility will help you to extract the file name from a given full file path in C#. Just pass the full file name as input to this function and you will get file in result.
This method find the last index of \\ and then extract the string after that.
/// <summary>
/// Get the file part of a path.
/// </summary>
/// <param name="path">The path to process</param>
/// <returns>The file part of the path</returns>
public static string GetFileFromPath(string path)
{
string trimmed = path.Trim('\\');
int index = trimmed.LastIndexOf('\\');
if (index < 0)
{
return trimmed; // No directory, just a file name
}
return trimmed.Substring(index + 1);
}
0 comments:
Post a Comment