FileInfo fileInfo = new FileInfo("example.txt"); string extension = fileInfo.Extension.Substring(1); Console.WriteLine(extension); // output: "txt"
FileInfo fileInfo = new FileInfo("example.txt"); string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length); Console.WriteLine(fileName); // output: "example"In this example, we use the Name property to get the file name and we use the Substring method to remove the file extension from the name. These examples are part of the System.IO namespace in the .NET Framework Class Library.