andrew4582

FileNameToFileTypeConverter WPF

Mar 16th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. using System.Text;
  7. using System.Windows.Data;
  8.  
  9. namespace Core.UI.Converters
  10. {
  11.     public class FileNameToFileTypeConverter : IMultiValueConverter, IValueConverter
  12.     {
  13.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  14.         {
  15.             if (values != null && values.Length >= 2 && values[0] is string && values[1] is string)
  16.             {
  17.                 string title = (string)values[0];
  18.                 string extension = (string)values[1];
  19.                 return GetFileType(title, extension);
  20.             }
  21.             return null;
  22.         }
  23.  
  24.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  25.         {
  26.             throw new NotImplementedException();
  27.         }
  28.  
  29.  
  30.         //implements IValueConverter for just extension
  31.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  32.         {
  33.             if (value == null)
  34.                 return string.Empty;
  35.  
  36.             string extension = value.ToString();
  37.             string title = "fakefile";
  38.  
  39.             return FileNameToFileTypeConverter.GetFileType(title, extension);
  40.         }
  41.  
  42.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  43.         {
  44.             throw new NotImplementedException();
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Gets the windows file type associated with a file's extension
  49.         /// </summary>
  50.         public static string GetFileType(string title, string extension)
  51.         {
  52.             if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(extension))
  53.                 return string.Empty;
  54.  
  55.             string fileName = string.Format("{0}.{1}", title, extension);
  56.             SafeNativeMethods.SHFILEINFO info = new SafeNativeMethods.SHFILEINFO();
  57.             uint dwFileAttributes = SafeNativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL;
  58.             uint uFlags = (uint)(SafeNativeMethods.SHGFI.SHGFI_TYPENAME | SafeNativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES);
  59.             SafeNativeMethods.SHGetFileInfo(fileName, dwFileAttributes, ref info, (uint)Marshal.SizeOf(info), uFlags);
  60.             return info.szTypeName;
  61.         }
  62.  
  63.         [SuppressUnmanagedCodeSecurity]
  64.         internal static class SafeNativeMethods
  65.         {
  66.             [StructLayout(LayoutKind.Sequential)]
  67.             public struct SHFILEINFO
  68.             {
  69.                 public IntPtr hIcon;
  70.                 public int iIcon;
  71.                 public uint dwAttributes;
  72.                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  73.                 public string szDisplayName;
  74.                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  75.                 public string szTypeName;
  76.             };
  77.  
  78.             public static class FILE_ATTRIBUTE
  79.             {
  80.                 public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
  81.             }
  82.  
  83.             public static class SHGFI
  84.             {
  85.                 public const uint SHGFI_TYPENAME = 0x000000400;
  86.                 public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
  87.             }
  88.  
  89.             [DllImport("shell32.dll")]
  90.             internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment