Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- using System.Windows.Data;
- namespace Core.UI.Converters
- {
- public class FileNameToFileTypeConverter : IMultiValueConverter, IValueConverter
- {
- public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- if (values != null && values.Length >= 2 && values[0] is string && values[1] is string)
- {
- string title = (string)values[0];
- string extension = (string)values[1];
- return GetFileType(title, extension);
- }
- return null;
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- //implements IValueConverter for just extension
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- if (value == null)
- return string.Empty;
- string extension = value.ToString();
- string title = "fakefile";
- return FileNameToFileTypeConverter.GetFileType(title, extension);
- }
- public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Gets the windows file type associated with a file's extension
- /// </summary>
- public static string GetFileType(string title, string extension)
- {
- if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(extension))
- return string.Empty;
- string fileName = string.Format("{0}.{1}", title, extension);
- SafeNativeMethods.SHFILEINFO info = new SafeNativeMethods.SHFILEINFO();
- uint dwFileAttributes = SafeNativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL;
- uint uFlags = (uint)(SafeNativeMethods.SHGFI.SHGFI_TYPENAME | SafeNativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES);
- SafeNativeMethods.SHGetFileInfo(fileName, dwFileAttributes, ref info, (uint)Marshal.SizeOf(info), uFlags);
- return info.szTypeName;
- }
- [SuppressUnmanagedCodeSecurity]
- internal static class SafeNativeMethods
- {
- [StructLayout(LayoutKind.Sequential)]
- 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;
- };
- public static class FILE_ATTRIBUTE
- {
- public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
- }
- public static class SHGFI
- {
- public const uint SHGFI_TYPENAME = 0x000000400;
- public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
- }
- [DllImport("shell32.dll")]
- internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment