Getting the file extension in C# is very simple,
FileInfo file = new FileInfo("c:\\myfile.txt");
MessageBox.Show(file.Extension); // Displays '.txt'
However I have files in my app with multiple periods.
FileInfo file = new FileInfo("c:\\scene_a.scene.xml");
MessageBox.Show(file.Extension); // Displays '.xml'
I want to be able to extract the .scene.xml
portion of the name.
Update
The extension should also include the initial .
How can I get this from FileInfo
?
You can use this regex to extract all characters after dot symbol:
\..*
var result = Regex.Match(file.Name, @"\..*").Value;
The .xml is the extension the filename is scene_a.scene
If you want to extract scene.xml. You'll need to parse it yourself.
Something like this might do what you want (you'll need to add more code to check for conditions where there isn't a . in the name at all.):
String filePath = "c:\\scene_a.scene.xml";
String myIdeaOfAnExtension = String.Join(".", System.IO.Path.GetFileName(filePath)
.Split('.')
.Skip(1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With