1

As i am able get Correct file Type for image as JPEG Image. But i can get file type for pdf document or sql file. I am using below code:

public String Type
{
      get
      {
            return GetType(Path.GetExtension(_document.DocumentPath));
      }
 }
public static string ReadDefaultValue(string regKey)
{
     using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(regKey, false))
     {
       if (key != null)
       {
           return key.GetValue("") as string;
       } 
     }
     return null;
}

public string GetType(string ext)
{
     if (ext.StartsWith(".") && ext.Length > 1) ext = ext.Substring(1);
     var retVal = ReadDefaultValue(ext + "file");
     if (!String.IsNullOrEmpty(retVal)) return retVal;

     using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("." + ext, false))
     {
        if (key == null) return "";
          using (var subkey = key.OpenSubKey("OpenWithProgids"))
          {
                    if (subkey == null) return "";

                    var names = subkey.GetValueNames();
                    if (names == null || names.Length == 0) return "";

                    foreach (var name in names)
                    {
                        retVal = ReadDefaultValue(name);
                        if (!String.IsNullOrEmpty(retVal)) return retVal;
                    }
                }
            }

            return "";
        }

As i have seen that there is no "OpenWithProgids" subkey in .pdf file in regedit. so what can do to get these file types.

For Example In win 7enter image description here

file type listed with file name and with other information , I want that same file type in my application I am able to xps document but not other document

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Do you mean you want to get the extension of a file? For example: `Filename.png` would return `.png`? – Rohan Sep 02 '13 at 04:34
  • No i want that if file is .png then it return PNG Image like win 7 –  Sep 02 '13 at 04:35
  • @khushbu, so...you if a file type is of .png, you want to display that image in your application? – Rohan Sep 02 '13 at 04:38
  • @khushbu The value of the `(default)` subkey of the `HKCR\.pdf` on my box has the value `AcroExch.Document`. In turn `HKCR\AcroExch.Document\(default)` says "Adobe Acrobat Document". No need to even go into an `OpenWithProgIds` subkey. However, I'm not sure that this is the right way to figure the "file type name" in the first place. – Christian.K Sep 02 '13 at 04:41

1 Answers1

2

You can get type using windows API SHGetFileInfo function

        [Flags]
        private enum SHGFI : int
        {
            /// <summary>get type name</summary>
            TypeName = 0x000000400,
        }

        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi,
            uint cbFileInfo, uint uFlags);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName;
        };

        private static void Main()
        {
            SHGFI flags = SHGFI.TypeName;
            SHFILEINFO shinfo = new SHFILEINFO();
           SHGetFileInfo(your path, 0,
                ref shinfo, (uint) Marshal.SizeOf(shinfo),(uint) flags);

            Console.WriteLine(shinfo.szTypeName);

            Console.ReadKey();
        }
Alyafey
  • 1,455
  • 3
  • 15
  • 23
  • this gives me exception as The runtime has encountered a fatal error. The address of the error was at 0x656ee556, on thread 0xac. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. –  Sep 02 '13 at 04:58
  • I had never got this error before I'm using both Visual Studio 2010 and Visual Studio 2012 and it works correctly with no errors – Alyafey Sep 02 '13 at 05:10
  • Could the errors occurs because of this http://social.msdn.microsoft.com/Forums/en-US/72238339-3b16-4d09-93ee-e84401e6681b/native-exception-excpetion-code-0xc0000005 – Alyafey Sep 02 '13 at 05:19
  • Now its suddenly shut down my application due to error –  Sep 02 '13 at 05:29
  • could you post the rest of your code could the problem somewhere else – Alyafey Sep 02 '13 at 05:56