How can I just get the base filename from this C# code?

You can use the FileInfo class:

FileInfo fi = new FileInfo(file);
string name = fi.Name;

If you want just the file name - quick and simple - use Path:

string name = Path.GetFileName(file);

You can use the following method: Path.GetFileName(file)


System.IO.FileInfo f = new System.IO.FileInfo(@"C:\pagefile.sys");  // Sample file.
System.Windows.Forms.MessageBox.Show(f.FullName);  // With extension.
System.Windows.Forms.MessageBox.Show(System.IO.Path.GetFileNameWithoutExtension(f.FullName));  // What you wants.

Tags:

C#