使用 C# 快速获取 MP3 文件信息的方法

Bittersweet 转载2016-3-22
本文最后更新于 2617 天前(2016-3-22),其中的信息可能已经有所发展或者不再适用于现阶段。
本文全长 588 字,全部读完大约需要 2 分钟。

如题,本文将介绍使用 C# 快速获取 MP3 文件信息的两种方法。

方法 1(兼容性较好):

[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);

[DllImport("winmm.dll")]
public static extern int mciSendString(string m_strCmd, StringBuilder m_strReceive, int m_v1, int m_v2);
public string getMp3Length(string file)
{
  if (File.Exists(file)) //是否存在这个文件
  {
    //利用MCI命令,返回值为文件时间
    //StringBuilder shortpath = new StringBuilder(80);
    //GetShortPathName(file, shortpath, shortpath.Capacity);
    // string musicname = shortpath.ToString();

    StringBuilder buf = new StringBuilder(80);
    mciSendString("close all", buf, buf.Capacity, 0);
    mciSendString("open " + file + " alias media", buf, buf.Capacity, 0);
    mciSendString("status media length", buf, buf.Capacity, 0);
    // MessageBox.Show(buf.ToString());
    double ms = Convert.ToDouble(buf.ToString().Trim());
    TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)ms);
    mciSendString("close all", buf, buf.Capacity, 0);
    return ts.ToString().Substring(0, 8); //这里你自己去决定返回的是什么格式
  }
  // 如果文件不存在就返回"00:00:00″
  return "00:00:00";
}

方法2(在不同版本的操作系统上可能出现兼容性问题):

首先引用 COM 组件 Microsoft Shell Controls And Automation。这里需要注意 DLL 的属性 Embed Interop Type 设为 false,否则会引起互操作类型异常。

代码如下

ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(Path.GetDirectoryName(sFile));
FolderItem item = dir.ParseName(Path.GetFileName(sFile));
string det = dir.GetDetailsOf(item, iCol);

iCol 对应文件详细属性汇总

ID  => DETAIL-NAME
0   => Name
1   => Size     // MP3 文件大小
2   => Type
3   => Date modified
4   => Date created
5   => Date accessed
6   => Attributes
7   => Offline status
8   => Offline availability
9   => Perceived type
10  => Owner
11  => Kinds
12  => Date taken
13  => Artists   // MP3 歌手
14  => Album     // MP3 专辑
15  => Year
16  => Genre
17  => Conductors
18  => Tags
19  => Rating
20  => Authors
21  => Title     // MP3 歌曲名
22  => Subject
23  => Categories
24  => Comments
25  => Copyright
26  => #
27  => Length    // MP3 时长
28  => Bit rate
29  => Protected
30  => Camera model
31  => Dimensions
32  => Camera maker
33  => Company
34  => File description
35  => Program name
36  => Duration
37  => Is online
38  => Is recurring
39  => Location
40  => Optional attendee addresses
41  => Optional attendees
42  => Organizer address
43  => Organizer name
44  => Reminder time
45  => Required attendee addresses
46  => Required attendees
47  => Resources
48  => Free/busy status
49  => Total size
50  => Account name
51  => Computer
52  => Anniversary
53  => Assistant's name
54  => Assistant's phone
55  => Birthday
56  => Business address
57  => Business city
58  => Business country/region
59  => Business P.O. box
60  => Business postal code
61  => Business state or province
62  => Business street
63  => Business fax
64  => Business home page
65  => Business phone
66  => Callback number
67  => Car phone
68  => Children
69  => Company main phone
70  => Department
71  => E-mail Address
72  => E-mail2
73  => E-mail3
74  => E-mail list
75  => E-mail display name
76  => File as
77  => First name
78  => Full name
79  => Gender
80  => Given name
81  => Hobbies
82  => Home address
83  => Home city
84  => Home country/region
85  => Home P.O. box
86  => Home postal code

本文在网上多篇文章(其原创作者较难考证)的基础上总结修改而来, 修改的部分按照 署名-相同方式共享 4.0 国际 许可授权。 如有侵权,请在下方留言