views:

439

answers:

2

Hi,

I am using SPFile to display a list of files and I want to display the type of file, as a string, so PDF, Word, Excel etc.

Looks like: FileABC PDF FileDEF Excel

I have got the icon back using "ListItem.File.IconUrl", ListItem is SPListItem

But I would like to get the file type as a name. It must work out the file type so the correct image can be displayed but I want to display the words too (for accessibility)

+1  A: 

You can simply take the "FileLeafRef" attribute which contains filename + local path, create an System.IO.FileInfo object out of that and then access the .Extension property:

System.IO.FileInfo inf = new System.IO.FileInfo("/images/files/something.gif");
Console.WriteLine(inf.Extension); //outputs .gif

I believe it will not be that easy to find matching program for each extension (you mention you need "Excel" not "xls". So, the easiest way, i think, is to create a dictionary of known extensions. Something like this:

System.Collections.Specialized.StringDictionary oDict = new System.Collections.Specialized.StringDictionary();
oDict.Add(".xls", "Excel");
oDict.Add(".xlsx", "Excel");
oDict.Add(".doc", "Word");

and then:

oDict[inf.Extension]
naivists
+1  A: 

Hi Marc

You can get the extension from the listitem as ListItem[SPBuiltInFieldId.DocIcon]

Per Jakobsen