Get File Name From a Path

Hi All,
while working on File System, Sometime we need to extract the file name from a given path. In dot Net it is very simple task as Framework library provide a specific class for doing these kind of task. “Path” is the class under the System.IO namespace has various methods for working on path.
GetFileName is the method of this class that extract the File name from the path.

[Visual Basic]
Dim fileName As String = “C:\mydir\myfile.ext”
Dim pathname As String = “C:\mydir\”
Dim result As String
result = Path.GetFileName(fileName)
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”, fileName, result)
result = Path.GetFileName(pathname)
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”, pathname, result)

[C#]
string fileName = @”C:\mydir\myfile.ext”;
string path = @”C:\mydir\”;
string result;
result = Path.GetFileName(fileName);
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”,
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine(“GetFileName(‘{0}’) returns ‘{1}'”,
path, result);

Advertisements