我正在设计一个媒体播放器,我已经设计了一个Movie类。Movie类有一个从MediaInfo继承的MovieInfo成员。MediaInfo有几个属性表示元数据(这就是您所说的吗?)例如文件长度、文件大小、文件路径等。为了提取这些信息,我使用了Shell32。
问题是Shell32中提供的方法非常非常慢。在我的数据库中有1部电影,这不是问题,但如果有10部电影,它就开始变得明显起来,如果有100部电影,程序加载大约需要5分钟,有几种情况下,我必须在运行时重新初始化电影,这再次停止了程序的流动。
MediaInfo构造函数调用Initialize方法:
/// <summary>
/// Initializes all the information variables of the class.
/// </summary>
private void Initialize()
{
Folder mediaFolder = Shell32Processing.GetShellFolder(this.path);
FolderItem media = Shell32Processing.GetShellFolderItem(this.path);
//initialize bit rate value
this.bitrate = mediaFolder.GetDetailsOf(media, 28);
//initialize date accessed value
this.dateAccessed = mediaFolder.GetDetailsOf(media, 5);
//initialize date created value
this.dateCreated = mediaFolder.GetDetailsOf(media, 4);
//initialize date modified value
this.dateModified = mediaFolder.GetDetailsOf(media, 3);
//initialize data rate value
this.dataRate = mediaFolder.GetDetailsOf(media, 279);
//initialize file name value
this.fileName = mediaFolder.GetDetailsOf(media, 155);
//initialize file type value
this.fileType = mediaFolder.GetDetailsOf(media, 181);
//initialize folder value
this.folder = mediaFolder.GetDetailsOf(media, 177);
//initialize folder name value
this.folderName = mediaFolder.GetDetailsOf(media, 175);
//initialize folder path value
this.folderPath = mediaFolder.GetDetailsOf(media, 176);
//initialize frame height value
this.frameHeight = mediaFolder.GetDetailsOf(media, 280);
//initialize frame rate value
this.frameRate = mediaFolder.GetDetailsOf(media, 281);
//initialize frame width value
this.frameWidth = mediaFolder.GetDetailsOf(media, 282);
//initialize length value
this.length = mediaFolder.GetDetailsOf(media, 27);
//initialize title value
this.title = FileProcessing.GetFileTitle(this.path);
//initialize total bitrate value
this.totalBitrate = mediaFolder.GetDetailsOf(media, 283);
//initialize size value
this.size = mediaFolder.GetDetailsOf(media, 1);
}下面是Shell32Processing.GetShellFolder方法:
/// <summary>
/// Gets a Shell32 Folder, initialized to the directory of the file path.
/// </summary>
/// <param name="path">A string representing the file/folder path</param>
/// <returns>The Shell32 Folder.</returns>
public static Folder GetShellFolder(string path)
{
//extract the folder subpath from the file path
//i.e extract "C:\Example" from "C:\Example\example.avi"
string directoryPath = new FileInfo(path).Directory.ToString();
return new Shell().NameSpace(directoryPath);
}和Shell32Processing.GetShellFolderItem方法:
/// <summary>
/// Gets a Shell32 FolderItem, initialized to the item specified in the file path.
/// </summary>
/// <param name="path">A string representing the file path.</param>
/// <returns>The Shell32 FolderItem.</returns>
/// <exception cref="System.ArgumentException">Thrown when the path parameter does not lead to a file.</exception>
public static FolderItem GetShellFolderItem(string path)
{
if (!FileProcessing.IsFile(path))
{
throw new ArgumentException("Path did not lead to a file", path);
}
int index = -1;
//get the index of the path item
FileInfo info = new FileInfo(path);
DirectoryInfo directoryInfo = info.Directory;
for (int i = 0; i < directoryInfo.GetFileSystemInfos().Count(); i++)
{
if (directoryInfo.GetFileSystemInfos().ElementAt(i).Name == info.Name) //we've found the item in the folder
{
index = i;
break;
}
}
return GetShellFolder(path).Items().Item(index);
}每次对GetDetailsOf (这是Shell32中提供的代码)的调用都需要花费难以置信的时间来处理-我使用ANTS分析器来找到这一点,因为一开始我不能确定是什么让我的程序变慢了这么多。
所以问题是:我如何优化Shell32方法,如果我不能,有没有替代方法?
发布于 2012-06-09 01:19:41
您在代码中做了许多错误的事情。您声称问题出在您提供的代码之外,但也许我们可以通过修复损坏的代码来解决问题。
public static Folder GetShellFolder(string path)
{
//extract the folder subpath from the file path
//i.e extract "C:\Example" from "C:\Example\example.avi"
string directoryPath = new FileInfo(path).Directory.ToString();
return new Shell().NameSpace(directoryPath);
}您访问文件系统只是为了获取文件路径的目录部分(new FileInfo(path).Directory)。您可以在不使用System.IO.Path.GetDirectoryName(path)命中磁盘驱动器的情况下执行此操作。
每次开始处理新项目时,都会创建一个新的shell对象。我会创建一个,处理所有的项目,然后释放它。因此,让我们像这样更改GetShellFolder:
public static Folder GetShellFolder(Shell shell, string path)
{
//extract the folder subpath from the file path
//i.e extract "C:\Example" from "C:\Example\example.avi"
string directoryPath = System.IO.Path.GetDirectoryName(path);
return shell.NameSpace(directoryPath);
}并向您的Initialize方法传递一个Shell对象。接下来是GetShellFolderItem。下面是你的代码:
public static FolderItem GetShellFolderItem(string path)
{
if (!FileProcessing.IsFile(path))
{
throw new ArgumentException("Path did not lead to a file", path);
}
int index = -1;
//get the index of the path item
FileInfo info = new FileInfo(path);
DirectoryInfo directoryInfo = info.Directory;
for (int i = 0; i < directoryInfo.GetFileSystemInfos().Count(); i++)
{
if (directoryInfo.GetFileSystemInfos().ElementAt(i).Name == info.Name) //we've found the item in the folder
{
index = i;
break;
}
}
return GetShellFolder(path).Items().Item(index);
}第一个错误是在访问该文件之前使用“文件是否存在”。不要这样做。只需访问该文件,如果该文件不存在,则会发生FileNotFoundException。你所做的就是添加已经完成的额外工作。无论你是否这样做,它仍然有可能通过“文件存在测试”,但无法访问。
接下来,您将解析目录以获取文件夹中文件的索引。这是一个严重的竞争情况。你完全有可能在这里得到错误的索引值。这也不是必需的,因为Folder公开了一个方法来按名称获取FolderItem:ParseName。
最后,您创建了另一个Folder (通过调用GetShellFolder),它也创建了另一个Shell项。您已经有一个Folder,请使用它。
因此,我们可以通过完全删除GetShellFolderItem来更改它:
FolderItem media = mediaFolder.ParseName(System.IO.Path.GetFileName(path));我们也可以干净利落地摆脱GetShellFolder:
private void Initialize(Shell shell)
{
Folder mediaFolder = null;
FolderItem media = null;
try
{
mediaFolder = shell.NameSpace(Path.GetDirectoryName(path));
media = mediaFolder.ParseName(Path.GetFileName(path));
...
}
finally
{
if (media != null)
Marshal.ReleaseComObject(media);
if (mediaFolder != null)
Marshal.ReleaseComObject(mediaFolder);
}
}让我们看看所有这些都有多大的不同。
您还可以调用GetDetailsOf来获取您已经知道或可以从媒体对象获取的内容:
//initialize folder name value
this.folderName = mediaFolder.GetDetailsOf(media, 175);
//initialize folder path value
this.folderPath = mediaFolder.GetDetailsOf(media, 176);
//initialize size value
this.size = mediaFolder.GetDetailsOf(media, 1);将这些更改为:
//initialize folder path value
this.folderPath = Path.GetDirectoryName(path);
//initialize folder name value
this.folderName = Path.GetFileName(folderPath);
//initialize size value
this.size = media.Size;发布于 2013-02-27 20:03:40
检查此Link。根据Win-OS版本的不同,您将获得更多关于GetDetailsOf()及其文件属性的信息。
List<string> arrHeaders = new List<string>();
Shell shell = new ShellClass();
Folder rFolder = shell.NameSpace(_rootPath);
FolderItem rFiles = rFolder.ParseName(filename);
for (int i = 0; i < short.MaxValue; i++)
{
string value = rFolder.GetDetailsOf(rFiles, i).Trim();
arrHeaders.Add(value);
}希望这能对某些人有所帮助。
https://stackoverflow.com/questions/10950477
复制相似问题