Given a filesystem path, is there a shorter way to extract the filename without its extension?

436,867

Solution 1

Path.GetFileName

Returns the file name and extension of a file path that is represented by a read-only character span.


Path.GetFileNameWithoutExtension

Returns the file name without the extension of a file path that is represented by a read-only character span.


The Path class is wonderful.

Solution 2

try

fileName = Path.GetFileName (path);

http://msdn.microsoft.com/de-de/library/system.io.path.getfilename.aspx

Solution 3

try

System.IO.Path.GetFileNameWithoutExtension(path); 

demo

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    fileName, result);

result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", 
    path, result);

// This code produces output similar to the following:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''

https://msdn.microsoft.com/en-gb/library/system.io.path.getfilenamewithoutextension%28v=vs.80%29.aspx

Solution 4

You can use Path API as follow:

 var filenNme = Path.GetFileNameWithoutExtension([File Path]);

More info: Path.GetFileNameWithoutExtension

Solution 5

var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);

Path.GetFileNameWithoutExtension

Share:
436,867

Related videos on Youtube

KMC
Author by

KMC

Tutorials: WPF C# Databinding C# Programming Guide .NET Framework Data Provider for SQL Server Entity Framework LINQ-to-Entity The Layout System Resources: LifeCharts: free .NET charts FileHelper: text file helper cmdow

Updated on March 16, 2021

Comments

  • KMC
    KMC about 3 years

    I program in WPF C#. I have e.g. the following path:

    C:\Program Files\hello.txt
    

    and I want to extract hello from it.

    The path is a string retrieved from a database. Currently I'm using the following code to split the path by '\' and then split again by '.':

    string path = "C:\\Program Files\\hello.txt";
    string[] pathArr = path.Split('\\');
    string[] fileArr = pathArr.Last().Split('.');
    string fileName = fileArr.Last().ToString();
    

    It works, but I believe there should be shorter and smarter solution to that. Any idea?

    • jbyrd
      jbyrd about 6 years
      In my system, Path.GetFileName("C:\\dev\\some\\path\\to\\file.cs") is returning the same string and not converting it to "file.cs" for some reason. If I copy/paste my code into an online compiler (like rextester.com), it works...?
  • bhuvin
    bhuvin over 8 years
    +1 since this might be Helpful in the case wherein this works as a Backup wherein the file name contains invalid characters [ <, > etc in Path.GetInvalidChars()].
  • Nolmë Informatique
    Nolmë Informatique over 5 years
    It seems Path.GetFileNameWithoutExtension() is not working with a file extension > 3 characters.
  • s952163
    s952163 almost 5 years
    This is actually quite useful when working with path on UNIX ftp servers.
  • Lance U. Matthews
    Lance U. Matthews about 4 years
    The question is asking how to extract the file name without extension from a file path. This, instead, is retrieving the immediate child files of a directory that may or may not be specified by a configuration file. Those are not really close to the same thing.
  • Lance U. Matthews
    Lance U. Matthews about 4 years
    I'm surprised that FileVersionInfo can be used on files that have no version information. Note that GetVersionInfo() can only be used on paths that reference a file that already exists. While either class can be used to get the file name, the question also asked to remove the extension, too.