[c#] Obtaining only the filename when using OpenFileDialog property "FileName"

I am trying to include only the filename of the file I've selected in the OpenFileDialog in the label1.Text property, but I haven't found a solution yet. I know I could use a method from the string class on the ofd instance to filter out the whole path to the file, but I would like to know if a smarter/quicker way exists?

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Find song";
ofd.Filter = "MP3 files|*.mp3";
ofd.InitialDirectory = @"C:\";
if (ofd.ShowDialog() == DialogResult.OK)
{
   label1.Text = "" + ofd.FileName +"";
}

This question is related to c# .net filenames openfiledialog

The answer is


Use OpenFileDialog.SafeFileName

OpenFileDialog.SafeFileName Gets the file name and extension for the file selected in the dialog box. The file name does not include the path.


Use: Path.GetFileName Method

var onlyFileName = System.IO.Path.GetFileName(ofd.FileName);

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to filenames

Rename multiple files in a folder, add a prefix (Windows) How to loop over files in directory and change path and add suffix to filename Why do I get a SyntaxError for a Unicode escape in my file path? Git copy file preserving history A html space is showing as %2520 instead of %20 How do I get the file name from a String containing the Absolute file path? DateTime.ToString() format that can be used in a filename or extension? Get only filename from url in php without any variable values which exist in the url Obtaining only the filename when using OpenFileDialog property "FileName" Build the full path filename in Python

Examples related to openfiledialog

Open a workbook using FileDialog and manipulate it in Excel VBA How to get file path from OpenFileDialog and FolderBrowserDialog? Open File Dialog, One Filter for Multiple Excel Extensions? Reading a text file using OpenFileDialog in windows forms How to use OpenFileDialog to select a folder? Open file dialog and select a file using WPF controls and C# Quick and easy file dialog in Python? Obtaining only the filename when using OpenFileDialog property "FileName" Load a bitmap image into Windows Forms using open file dialog Multiple file extensions in OpenFileDialog