Link to home
Start Free TrialLog in
Avatar of divteam
divteam

asked on

I want to find Content Type based on File Extention. Is there any in build function in C# or can anyone guide me how to do that?

I want to find Content Type based on File Extention. Is there any in build function in C# or can anyone guide me how to do that?
Avatar of almander
almander

You can look in the registry in HKEY_CLASSES_ROOT  for the extension and read the REG_SZ value that identifies the content type.
Here are some classes that return the file association in the OS :
http://www.codeproject.com/KB/dotnet/System_File_Association.aspx
If you are referring to content type as binary or string then you could test with readers in the IO namespace.
 
When you say 'Content Type', is that type of file e.g. Word Document vs Web pages vs XML documents etc.?
Extension and those types are stored in Windows Registry.  Try search some windows API  

Content type - file extension mapping is available in the registry in

\\HKEY_CLASSES_ROOT\MIME\Database\Content Type

For example

HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/pdf

has Extension value = .pdf.

You can write a c# method to get this type from file extension.
Hi,

Check code. I am trying to find .xml files. Modify it as your requirements.
Regards,
V.S.Saini

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ee_XmlFileArray
{
    public partial class Form1 : Form
    {
        ArrayList arrFiles = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {            
            DirectoryInfo dir = new DirectoryInfo(@"D:\Zodes");

            foreach (FileInfo file in dir.GetFiles())
            {
                if (file.Extension.Equals(".xml"))
                {
                    arrFiles.Add(file.Name);            
                }               
            }

            if (arrFiles.Count > 0)
            {
                for (int i = 0; i < arrFiles.Count; i++)
                {
                    rtbFiles.Text += arrFiles[i].ToString() + "|";
                }
            }
        }        
    }
}

Open in new window

Avatar of divteam

ASKER

I am trying to upload file from outlook email into database and I need to find ContentType. I do have file extention but it does not seem to be working fine. Even though it upload successfully, when i try to view the attachment, it always error out with message saying "invalid format".

Any idea how to upload email attachments into Database?

Note:- I am using SQL 2000 and storing as image.

byte[] data = new byte[item.Attachments[a].Size];
                                int bytes_read = 0;

                                sql = @"insert into bug_post_attachments
                                          (bpa_post, bpa_content)
                                          values (@bp, @bc)";
                                using (SqlCommand cmd = new SqlCommand(sql))
                                {
                                    cmd.Parameters.AddWithValue("@bp", bp_id);
                                    cmd.Parameters.Add("@bc", SqlDbType.Image).Value = data;
                                    DbUtil.execute_nonquery(cmd);
                                }
upload-error.bmp
ASKER CERTIFIED SOLUTION
Avatar of kris_per
kris_per

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial