473,379 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Detect file type

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
Aug 4 '06 #1
7 33518
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

................
}


"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

Aug 4 '06 #2
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
=====================
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Aug 4 '06 #3
Hi,

What you want to do this this?

In the registry you can find the program registerd to handle a given
extension.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

Aug 4 '06 #4
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/sear...oup%3A*dotnet*
/claes

"Mihai N." <nm**************@yahoo.comwrote in message
news:Xn*******************@207.46.248.16...
>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
=====================
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Aug 4 '06 #5
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.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

What you want to do this this?

In the registry you can find the program registerd to handle a given
extension.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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


Aug 4 '06 #6
Sorry, what does it returns?

'.doc', '.txt' ?

"Kevin Spencer" <uc*@ftc.govwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
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.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi,

What you want to do this this?

In the registry you can find the program registerd to handle a given
extension.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>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



Aug 6 '06 #7

"Alan T" <al*************@yahoo.com.auwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
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 ;)
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I have a windows 2000 PC and it does not know what a hta file is. any ideas. Mark
4
by: CLEAR-RCIC | last post by:
In the old days, you could get a file type in ASP by doing: dim fs, fld, f set fs = server.createobject("Scripting.FileSystemObject") set fld = fs.GetFolder(server.mappath(sCurrentDir)) for...
2
by: Brian Henry | last post by:
I want to list out a directory listing along with showing the file type name (like explorer does when it says something like "MyDoc.DOC - Microsoft Word Document" How do I get that file type name...
5
by: RC | last post by:
how to detect file is completely downloaded from client by following code? <%@ Page language="C#" debug="true" %> <script language="C#" runat="server"> private void Page_Load(object sender,...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
1
by: RobinSword | last post by:
Hi there! I want my VB.NET-program to detect CPU type and speed like: "Intel Pentium 3,06 GHz". How can I do that with VB.NET ? If there is the answer somewhere in the forum: Sorry, I didn't...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
2
Nepomuk
by: Nepomuk | last post by:
Recently, I found that I had associated a file type to notepad by mistake. This file type (let's just say, it has the extension .xyz) wasn't supposed to be opened by notepad - actually, it wasn't...
1
by: sasimca007 | last post by:
Hai friends, I have a doubt that if we are having a page which having the functionality to upload a file, then if we want to restrict to some types of files only have to be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.