[C#]取得檔案內容中的詳細資料
using System.Linq;
using System.Text;
using Shell32;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ViewDetailColumn();
Console.WriteLine(new string(’=’, 50));
var file = @“C:\Users\Public\Music\Sample Music\Kalimba.mp3”;
ViewDetailValue(file, “Album”);
ViewDetailValue(file, “Size”);
}
static IEnumerable<KeyValuePair<string, int>> GetDetailColumn()
{
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(@“c:");
int idx = 0;
string columnName = dir.GetDetailsOf(0, idx);
do
{
yield return new KeyValuePair<string, int>(columnName, idx);
columnName = dir.GetDetailsOf(0, ++idx); } while (!string.IsNullOrEmpty(columnName));
}
static IEnumerable<KeyValuePair<string, int>> GetDetailColumn(int offset, int count)
{
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(@“c:");
for (var idx = offset; idx < offset + count; ++idx)
{
yield return new KeyValuePair<string, int>(dir.GetDetailsOf(0, idx), idx);
}
}
static void ViewDetailColumn()
{
var columns = GetDetailColumn();
foreach (var item in columns)
{
Console.WriteLine(item.Key);
}
}
static void ViewDetailColumn(int offset, int count)
{
var columns = GetDetailColumn(offset, count);
foreach (var item in columns)
{
Console.WriteLine(item.Key);
}
}
static string GetDetailValue(string file, int column)
{
ShellClass sh = new ShellClass();
Folder dir = sh.NameSpace(Path.GetDirectoryName(file));
FolderItem item = dir.ParseName(Path.GetFileName(file));
return dir.GetDetailsOf(item, column);
}
static void ViewDetailValue(string file, int column)
{
Console.WriteLine(GetDetailValue(file, column));
}
static string GetDetailValue(string file, string column)
{
var linq = from item in GetDetailColumn()
where item.Key == column
select item.Value;
return GetDetailValue(file, linq.FirstOrDefault());
}
static void ViewDetailValue(string file, string column)
{
Console.WriteLine(GetDetailValue(file, column));
}
}
}