Use the PATH class to extract the file name from the file path
When processing the file path, it is a common task to extract file name (excluding extension). Traditionally, developers may use string segmentation to achieve this, as shown in the following example:
<code class="language-csharp">string path = "C:\Program Files\hello.txt"; string[] pathArr = path.Split('\'); string[] fileArr = pathArr.Last().Split('.'); string fileName = fileArr.Last().ToString();</code>
Path.getFilename method
<code class="language-csharp">public static string GetFileName(string path);</code>
<code class="language-csharp">public static string GetFileNameWithoutExtension(string path);</code>
<code class="language-csharp">string path = "C:\Program Files\hello.txt"; string fileName = Path.GetFileNameWithoutExtension(path);</code>
The above is the detailed content of How to Easily Extract a Filename (Without Extension) from a Filepath in C#?. For more information, please follow other related articles on the PHP Chinese website!