gpt4 book ai didi

c# - 如何根据扩展名获取文件类型信息? (不是 MIME)在 c# 中

转载 作者:IT王子 更新时间:2023-10-29 04:47:09 28 4
gpt4 key购买 nike

如何像 Explorer 那样获取基于扩展名的一般文件类型描述?所以不是 MIME,而是最终用户看到的信息,例如。

.doc = Microsoft Office Word 97 - 2003 文档.zip = 压缩文件.avi = 视频文件。

我怎样才能获得似乎可用的“次要”信息,我猜它不是基于扩展的。就像在“视频文件”上一样,它可以为您提供电影的“长度”或在文档文件上它有多少页......等等......

最佳答案

谢谢 Dan,好的。这回答了我的第一个问题。可惜不是第二个。注意:并非所有内容都打印出来。归功于 PInvoke.net

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
static class Program
{
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Debug.WriteLine(FileExtentionInfo(AssocStr.Command, ".doc"), "Command");
Debug.WriteLine(FileExtentionInfo(AssocStr.DDEApplication, ".doc"), "DDEApplication");
Debug.WriteLine(FileExtentionInfo(AssocStr.DDEIfExec, ".doc"), "DDEIfExec");
Debug.WriteLine(FileExtentionInfo(AssocStr.DDETopic, ".doc"), "DDETopic");
Debug.WriteLine(FileExtentionInfo(AssocStr.Executable, ".doc"), "Executable");
Debug.WriteLine(FileExtentionInfo(AssocStr.FriendlyAppName, ".doc"), "FriendlyAppName");
Debug.WriteLine(FileExtentionInfo(AssocStr.FriendlyDocName, ".doc"), "FriendlyDocName");
Debug.WriteLine(FileExtentionInfo(AssocStr.NoOpen, ".doc"), "NoOpen");
Debug.WriteLine(FileExtentionInfo(AssocStr.ShellNewValue, ".doc"), "ShellNewValue");

// DDEApplication: WinWord
//DDEIfExec: Ñﻴ߾
// DDETopic: System
// Executable: C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE
// FriendlyAppName: Microsoft Office Word
// FriendlyDocName: Microsoft Office Word 97 - 2003 Document


}

public static string FileExtentionInfo(AssocStr assocStr, string doctype)
{
uint pcchOut = 0;
AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut);

StringBuilder pszOut = new StringBuilder((int)pcchOut);
AssocQueryString(AssocF.Verify, assocStr, doctype, null, pszOut, ref pcchOut);
return pszOut.ToString();
}

[Flags]
public enum AssocF
{
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200
}

public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic
}

}
}

关于c# - 如何根据扩展名获取文件类型信息? (不是 MIME)在 c# 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/770023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com