Detect file type

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

Is there a way to determine what type of file it is?
eg.
MyDoc.doc returns Word
MyDoc.xls or MyDoc.csv returns Excel
MyDoc.ppt returns PowerPoint
.... etc
 
if u r suing .net 2.0 version then u can check ur file upload extension by
using

string extension = Path.GetExtension(FileInput.PostedFile.FileName);

switch(extension.tolower())
{
case ".doc": // do something

case ".jpg": // do something

................
}
 
if u r suing .net 2.0 version then u can check ur file upload extension by
using

string extension = Path.GetExtension(FileInput.PostedFile.FileName);

switch(extension.tolower())
{
case ".doc": // do something

case ".jpg": // do something

...............
}
..jpg has various applications associated with it, depending on the system.
You cannot hard-code it in the sources.


I am not sure if there is an API for this, but the info is in regitry, in
HKEY_CLASSES_ROOT.
Take .rtf for instance and go to HKEY_CLASSES_ROOT\.rtf:
=====================
[HKEY_CLASSES_ROOT\.rtf]
@="Word.RTF.8"
"Content Type"="application/msword"

[HKEY_CLASSES_ROOT\.rtf\OpenWithList\WordPad.exe]
@=""

[HKEY_CLASSES_ROOT\.rtf\PersistentHandler]
@="{5e941d80-bf96-11cd-b579-08002b30bfeb}"

[HKEY_CLASSES_ROOT\.rtf\ShellNew]
"Data"="{\\rtf1}"
=====================
Then you go to Word.RTF.8 and see the various shell commands:
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Edit
HKEY_CLASSES_ROOT\Word.RTF.8\shell\New
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Open
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Print
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Printto
=====================
 
Hi,

What you want to do this this?

In the registry you can find the program registerd to handle a given
extension.
 
You can P/Invoke SHGetFileInfo to get this info if you don't feel like
reading the registry directly. You'll most likely be able to find all the
necessary stuff in one of these posts:
http://groups.google.com/groups/search?q=SHGetFileInfo+group:*dotnet*


/claes

Mihai N. said:
if u r suing .net 2.0 version then u can check ur file upload extension
by
using

string extension = Path.GetExtension(FileInput.PostedFile.FileName);

switch(extension.tolower())
{
case ".doc": // do something

case ".jpg": // do something

...............
}
.jpg has various applications associated with it, depending on the system.
You cannot hard-code it in the sources.


I am not sure if there is an API for this, but the info is in regitry, in
HKEY_CLASSES_ROOT.
Take .rtf for instance and go to HKEY_CLASSES_ROOT\.rtf:
=====================
[HKEY_CLASSES_ROOT\.rtf]
@="Word.RTF.8"
"Content Type"="application/msword"

[HKEY_CLASSES_ROOT\.rtf\OpenWithList\WordPad.exe]
@=""

[HKEY_CLASSES_ROOT\.rtf\PersistentHandler]
@="{5e941d80-bf96-11cd-b579-08002b30bfeb}"

[HKEY_CLASSES_ROOT\.rtf\ShellNew]
"Data"="{\\rtf1}"
=====================
Then you go to Word.RTF.8 and see the various shell commands:
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Edit
HKEY_CLASSES_ROOT\Word.RTF.8\shell\New
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Open
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Print
HKEY_CLASSES_ROOT\Word.RTF.8\shell\Printto
=====================
 
Here's a little method I wrote at one time to do this. It returns a string:

public static string GetFileType(string ext)
{
RegistryKey rKey = null;
RegistryKey sKey = null;
string FileType = "";

try
{
rKey = Registry.ClassesRoot;
sKey = rKey.OpenSubKey(ext);
if (sKey != null && (string)sKey.GetValue("", ext) != ext)
{
sKey = rKey.OpenSubKey((string)sKey.GetValue("", ext));
FileType = (string)sKey.GetValue("");
}
else
FileType = ext.Substring(ext.LastIndexOf('.') + 1).ToUpper() + " File";
return FileType;
}
finally
{
if (sKey != null) sKey.Close();
if (rKey != null) rKey.Close();
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.
 
Sorry, what does it returns?

'.doc', '.txt' ?

Kevin Spencer said:
Here's a little method I wrote at one time to do this. It returns a
string:

public static string GetFileType(string ext)
{
RegistryKey rKey = null;
RegistryKey sKey = null;
string FileType = "";

try
{
rKey = Registry.ClassesRoot;
sKey = rKey.OpenSubKey(ext);
if (sKey != null && (string)sKey.GetValue("", ext) != ext)
{
sKey = rKey.OpenSubKey((string)sKey.GetValue("", ext));
FileType = (string)sKey.GetValue("");
}
else
FileType = ext.Substring(ext.LastIndexOf('.') + 1).ToUpper() + " File";
return FileType;
}
finally
{
if (sKey != null) sKey.Close();
if (rKey != null) rKey.Close();
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Who is Mighty Abbott? A twin-turret scalawag.
 
Alan T said:
Sorry, what does it returns?

'.doc', '.txt' ?

FileType = ext.Substring(ext.LastIndexOf('.') + 1).ToUpper() + " File";
return FileType;


Those two lines will give you the answer ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Cant open section of Word Document 1
unbound object 2
PowerPoint High Resolution Images from PowerPoint 0
Start function 15
Hyperlink - relative 2
Conditional Expression 4
Just File Name 6
How to find default value for value type 2

Back
Top