Search code examples
c#stringparsing

Remove file extension from a file name string


If I have a string saying "abc.txt", is there a quick way to get a substring that is just "abc"?

I can't do an fileName.IndexOf('.') because the file name could be "abc.123.txt" or something and I obviously just want to get rid of the extension (i.e. "abc.123").


Solution

  • I used the below, less code

    string fileName = "C:\file.docx";
    Path.Combine(Path.GetDirectoryName(fileName),
        Path.GetFileNameWithoutExtension(fileName));
    

    Output will be

    C:\file