Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file extension with a file that has multiple periods

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?

like image 890
IEnumerable Avatar asked Feb 11 '14 03:02

IEnumerable


2 Answers

You can use this regex to extract all characters after dot symbol:

\..*

var result = Regex.Match(file.Name, @"\..*").Value;
like image 120
Kirill Polishchuk Avatar answered Oct 31 '22 10:10

Kirill Polishchuk


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));
like image 33
Daniel James Bryars Avatar answered Oct 31 '22 10:10

Daniel James Bryars