[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
?
511 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
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 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