zoukankan      html  css  js  c++  java
  • C# 获取视频文件播放时长

    以前是用xp (32位)系统,获取视频文件长度是通过调用Shell32.dll来读取文件信息得到的,最近换win7(32位)系统,编译以前的项目代码发现无法读取了,代码没有改动,拿到在xp下运行也不行了,可将代码在xp环境下编译却可以成功运行。附上代码

    /// <summary>
    /// 获取媒体文件播放时长
    /// </summary>
    /// <param name="path">媒体文件路径</param>
    /// <returns></returns>
    public string GetMediaTimeLen(string path) {
        try
        {
     
            Shell32.Shell shell = new Shell32.ShellClass();
            //文件路径
            Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
            //文件名称
            Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
     
            return folder.GetDetailsOf(folderitem, 21);             
     
        }
        catch(Exception ex)
        {
            return null;
        }
       
    }

    网上搜索查到xp 与win7的Shell32.dll是不一样的,要将参数设27,可以读取视频播放长度,但拿到xp下却不可以,在xp引用的就不能再win7下读取,通过visual studio引用后变成Interop.Shell32.dll,我在xp下引用的是windows目录下的System32目录的Shell32.dll,在win7下编译目录还是windows目录下的System32目录的Shell32.dll,2者版本不一致,于是拷贝xp下Shell32.dll到win7 项目下,并引用,编译,可以成功读取(但xp下GetDetailsOf的参数是21,win7下的是27)。于是先判断下系统版本

    Windows操作系统的版本号一览

    操作系统 PlatformID 主版本号 副版本号
    Windows95 1 4 0
    Windows98 1 4 10
    WindowsMe 1 4 90
    WindowsNT3.5 2 3 0
    WindowsNT4.0 2 4 0
    Windows2000 2 5 0
    WindowsXP 2 5 1
    Windows2003 2 5 2
    WindowsVista 2 6 0
    Windows7 2 6 1

    于是一行代码识别系统版本Environment.OSVersion.Version.Major >= 6,vista、win7 用参数27,xp、2003用21

    public string GetMediaTimeLen(string path) {
                try
                {
     
                    Shell32.Shell shell = new Shell32.ShellClass();
                    //文件路径
                    Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                    //文件名称
                    Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
                    if(Environment.OSVersion.Version.Major >= 6)
                    {
                        return folder.GetDetailsOf(folderitem, 27);
                    }
                    else
                    {
                        return folder.GetDetailsOf(folderitem, 21);
                    }
                   
     
                }
                catch(Exception ex)
                {
                    return null;
                }
               
            }

    现在既可以在xp下运行,也可以在win7下运行。(注意:64位系统win7不可用,暂未找到好的解决方法,望有成功的提供方法。)

  • 相关阅读:
    在Ubuntu下安装软件
    HIVE Thrift Server使用总结
    用Wubi来安装Ubuntu 12
    linux命令总结
    Linux Sed简介
    Linux批量重命名文件
    算法学习的轨迹(转)
    写入es速率优化
    走近Flex组件系列(三):按扭组件(Button,CheckBox,LinkBar,LinkButton,PopUpButton,RadioButton,ToggleButtonBar)
    走近Flex组件系列(二):简单实用的Alert组件
  • 原文地址:https://www.cnblogs.com/ITBread/p/2500411.html
Copyright © 2011-2022 走看看