C# 无法获取特定文件的文件类型
因为我能够得到正确的文件类型的图像作为JPEG图像。但我可以得到pdf文档或sql文件的文件类型。 我正在使用以下代码:C# 无法获取特定文件的文件类型,c#,wpf,file-type,C#,Wpf,File Type,因为我能够得到正确的文件类型的图像作为JPEG图像。但我可以得到pdf文档或sql文件的文件类型。 我正在使用以下代码: public String Type { get { return GetType(Path.GetExtension(_document.DocumentPath)); } } public static string ReadDefaultValue(string regKey) { using (v
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 "";
}
正如我所看到的,regedit中的.pdf文件中没有“OpenWithProgId”子项。
那么,我们可以做些什么来获取这些文件类型呢
比如说
赢7
列出的文件类型包括文件名和其他信息,
我希望在我的应用程序中使用相同的文件类型
我可以创建xps文档,但不能创建其他文档您可以使用windows API函数获取类型
[标志]
私有枚举SHGFI:int
{
///获取类型名
TypeName=0x000000400,
}
[DllImport(“shell32.dll”,CharSet=CharSet.Auto)]
公共静态外部IntPtr SHGetFileInfo(字符串pszPath、uint dwFileAttributes、ref SHFILEINFO psfi、,
uint cbFileInfo,uint uFlags);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
公共结构SHFILEINFO
{
公共IntPtr hIcon;
公共国际会议;
公共单位属性;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=260)]公共字符串szDisplayName;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=80)]公共字符串szTypeName;
};
私有静态void Main()
{
SHGFI flags=SHGFI.TypeName;
SHFILEINFO shinfo=新的SHFILEINFO();
SHGetFileInfo(您的路径,0,
参考shinfo(uint)元帅SizeOf(shinfo)(uint)旗;
Console.WriteLine(shinfo.szTypeName);
Console.ReadKey();
}
您的意思是要获取文件的扩展名吗?例如:Filename.png
将返回.png
?不,如果文件为.png,则返回png图像,如win7@khushbu,因此…如果文件类型为.png,您希望在应用程序中显示该图像吗?@khushbu的值(默认值)
我的框中HKCR\.pdf
的子键的值为AcroExch.Document
。反过来HKCR\AcroExch.Document\(默认)
显示“AdobeAcrobat文档”。甚至不需要使用progids进入open
子键。但是,我不确定这是否是计算“文件类型名称”的正确方法。请参阅我编辑的问题这给了我一个例外,因为运行时遇到了致命错误。错误地址位于线程0xac上的0x656ee556。错误代码为0xc0000005。此错误可能是CLR或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括COM互操作或PInvoke的用户封送错误,这可能会损坏堆栈。在使用Visual Studio 2010和Visual Studio 2012之前,我从未遇到过此错误,它可以正常工作,没有错误。如果因此发生错误,现在它会因错误而突然关闭我的应用程序。您可以将其余代码发布到其他地方吗
[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();
}