We can pass SPFile.Name object into FileInfo Class and get the extension of file. It will returns “.ASPX” or “.HTML” file extension depending upon file object.
Example Code:
SPFile objFile;
string strFileExtension = new FileInfo(objFile.Name).Extension;
hi,
ReplyDeletegood trick but this can also be done with one more way
recently i was stuck with the same issue and i wanted the filename without extension and not the extension by itself..
one way was to take the extension and use substring in file name but a tedious way to so..
so i took the another approach and it was quite simple
using System.IO;
string filenamewitoutextension = Path.GetFileNameWithoutExtension(objFile.Name);
string extension = Path.GetExtension(objFile.Name);
hope this comes handy...
Enjoi..........
thanks.it helped me :)
ReplyDeleteGood trick, but we have to create file info object which may not be desirable all the time.
ReplyDeleteWe can use SPFile.Item["File Type"] to get extension.
Not all items have ["File Type"] field ! Path.GetExtension(objFile.Name) is better.
Delete