Thursday, May 3, 2012

Read Content Type from Registry , sort the dictionary and write into xml file using C#


If you want to read different extension and their content type using C# code . The following process will be helpful . It reads the extensions , their content type sorts the dictionary and writes the file in xml .


static void Main(string[] args)

        {

            GetFileTypeAndApplicationName();

        }

        private static void GetFileTypeAndApplicationName()

        {

            var fileTypes = new
Dictionary<string, string>();

            var RepeatedfileTypes = new
Dictionary<string, string>();

            try

            {          
  

                // Create a registry
key object to represent the HKEY_CLASSES_ROOT registry section

                var rkRoot =
Registry.ClassesRoot;

                // Gets all sub keys'
names.

                string[] keyNames =
rkRoot.GetSubKeyNames();

                // Find the file
extension and its application name

                foreach (string keyName
in keyNames)

                {

                    if
(String.IsNullOrEmpty(keyName))

                     
  continue;

                    int
indexOfPoint = keyName.IndexOf(".");

                    //If this
key is not a file exttension(eg, .zip), skip it.

                    if
(indexOfPoint != 0)

                     
  continue;

                    RegistryKey
rkFileType = rkRoot.OpenSubKey(keyName);

                    if
(rkFileType == null)

                     
  continue;

                    //Gets
the default value of this key that contains the information of file type.

                    object
defaultValue = rkFileType.GetValue("");

                    object
contentType = rkFileType.GetValue("Content Type");

                    if
(defaultValue == null)

                     
  continue;

                    if
(contentType != null)

                    {  
                  

                     
      fileTypes[keyName] = contentType.ToString();    
                

                    }  
              

                

                   
rkFileType.Close();

                }

                rkRoot.Close();

            }

            catch (Exception exc)

            {          


            } 

            // Use Linq to sort the dictionary
according to value

            var sortedDict = (from entry in
fileTypes orderby entry.Value ascending select entry).ToDictionary(pair =>
pair.Key, pair => pair.Value);

            using (XmlWriter writer =
XmlWriter.Create("D:\\MimeToExtension.xml"))

            {

               
writer.WriteStartDocument();

               
writer.WriteStartElement("MimeToExtensions");

                foreach
(KeyValuePair<string, string> mime in sortedDict)

                {

                   
writer.WriteStartElement("MimeToExtension");

                   
writer.WriteAttributeString("MimeType", mime.Value);

                   
writer.WriteAttributeString("Extension", mime.Key);

                   
writer.WriteEndElement();

                }

               
writer.WriteEndElement();

               
writer.WriteEndDocument();

            }

        

        }
    }

No comments:

Post a Comment