如何优化Shell32方法调用?

时间:2012-06-08 14:10:28

标签: c# optimization metadata shell32

我正在设计一个媒体播放器,我设计了一个电影课程。 Movie类有一个MovieInfo成员,它继承自MediaInfo。 MediaInfo有几个属性表示Movie文件的元数据(就是你所说的?),例如文件长度,文件大小,文件路径等。为了提取这些信息,我使用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方法,如果不能,还有其他方法吗?

2 个答案:

答案 0 :(得分:9)

您的代码中有很多错误。您声明问题出在您提供的代码之外,但也许我们可以通过修复已损坏的内容来解决问题。

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);
}

Shell对象传递给Initialize方法。接下来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;

答案 1 :(得分:0)

检查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);
 }

希望这对某些人有帮助。