?
Solved

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?

Posted on 2010-08-13
8
Medium Priority
?
508 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:divteam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:almander
ID: 33432594
You can look in the registry in HKEY_CLASSES_ROOT  for the extension and read the REG_SZ value that identifies the content type.
0
 
LVL 11

Expert Comment

by:ladarling
ID: 33432601
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.
 
0
 
LVL 5

Expert Comment

by:chinawal
ID: 33432609
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  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33432631
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33432672

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.
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33435008
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

0
 

Author Comment

by:divteam
ID: 33447491
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
0
 
LVL 16

Accepted Solution

by:
kris_per earned 2000 total points
ID: 33448034

I am not sure why do you want to find the content type or where you use it...

For the xlsx file to open, hope ms office 2007 or higher is installed in that machine.

If ms office 2007 is already installed, then it may be what the data bytes array contains...can you post the code that loads the attachment into data bytes array...

Below is a similar code that saves the file into database which is working fine...this can be compared with your code...
private void cmdSave_Click(object sender, EventArgs e)
{
    try
    {
          //Read Image Bytes into a byte array
          byte[] imageData = ReadFile(txtImagePath.Text);
                
          //Initialize SQL Server Connection
          SqlConnection CN = new SqlConnection(txtConnectionString.Text);

          //Set insert query
          // ImageData column is sql image type
          string qry = "insert into ImagesStore 
		(OriginalPath,ImageData) values(@OriginalPath, @ImageData)";

          //Initialize SqlCommand object for insert.
          SqlCommand SqlCom = new SqlCommand(qry, CN);

          //We are passing Original Image Path and 
          //Image byte data as SQL parameters.
          SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", 
			(object)txtImagePath.Text));
          SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData)); // ImageData column is sql image type


          //Open connection and execute insert query.
          CN.Open();
          SqlCom.ExecuteNonQuery();
          CN.Close();

          //Close form and return to list or images.
          this.Close();
      }


byte[] ReadFile(string sPath)
{
    //Initialize byte array with a null value initially.
    byte[] data = null;

    //Use FileInfo object to get file size.
    FileInfo fInfo = new FileInfo(sPath);
    long numBytes = fInfo.Length;

    //Open FileStream to read file
    FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

    //Use BinaryReader to read file stream into byte array.
    BinaryReader br = new BinaryReader(fStream);

    //When you use BinaryReader, you need to supply number of bytes 
    //to read from file.
    //In this case we want to read entire file. 
    //So supplying total number of bytes.
    data = br.ReadBytes((int)numBytes); 

    return data;
}

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question