前言:

  需要解压InfoPath表单的xsn文件,在项目中以前使用的是Expand命令行解压,都没有出过问题,近段时间项目中突然报错解压失败,通过分析解压操作得出结论:

    1.正常正常情况下,expand命令行解压没有任何问题,同一个站点,相同的请求,随机出现解压失败的错误。而且最容易复现的情况为:高频率刷新页面。

    2.监视解压的目标目录,解压失败的时候,目录没有任何变化。而解压成功时,目录监视则正常。

  然后将expand命令放到bat文件中,在bat文件中,执行expand命令之前,先执行 “md” 命令创建随机目录,C#代码代码执行bat命令,发现在解压失败的时候,bat命令即使执行完成,目录监视也没有发现md命令创建的目录。只能猜测C#在执行命令行的时候,某些情况下会存在不同步的情况。

  也没有时间专门去研究这个同步的问题,项目中有使用C#调用COM组件的地方,然后去网上搜了一下COM组件解压的cab文件的资料,发现使用shell32进行解压则没有问题。只是需要注意添加Shell32引用的方式:

  1.添加“Microsoft Shell Controls And Automation” 引用,如下图所示:

    

  2.生成项目,在bin目录下会生成“Interop.Shell32.dll”程序集,拷贝到其他目录,然后移除对Sell32的引用:

    

  3.添加对“Interop.Shell32.dll”程序集的引用,然后效果如下图所示:

    

  至于为什么要进行上述操作,是因为:直接添加对“Microsoft Shell...”的引用,代码生成之后在其他系统可能无法正常调用,如Win 2003 生成的无法在win2007上使用,但是通过上述方式引用之后,则可以了了。这样就可以正常使用Shell进行操作了。进行Shell操作的资料可以参考:http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/

  最终代码整理如下:代码中也包括cmd命令行的方式,在此供参考。

代码:

public partial class Extract : System.Web.UI.Page

    {

        /// <summary>

        /// 要解压的文件名称

        /// </summary>

        private String XSNFileName = @"infopath.xsn";

        /// <summary>

        /// 解压到....  的目标路径

        /// </summary>

        private String TargetDirectory = @"C:\xsn";

        /// <summary>

        /// cab文件名称

        /// </summary>

        private String CabFileName = "cab.cab";

        protected void Page_Load(object sender, EventArgs e)

        {

            //使用cmd命令解压

            this.ExtractByCmd();

            //使用shell32进行解压

            this.ExtractByShell();

        }

        #region cmd命令解压

        /// <summary>

        /// 使用cmd命令进行解压

        /// </summary>

        private void ExtractByCmd()

        {

            //使用cmd命令:expand  sourcefile  targetDir  -F:*    

            //  上面的命令得注意:目标目录不能是sourceFile的目录。

            System.Text.StringBuilder sbString = new System.Text.StringBuilder();

            String tempDir = Guid.NewGuid().ToString();

            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(this.TargetDirectory, tempDir));

            String cmdString = String.Format("\"{0}\" \"{1}\" -F:*", this.XSNFileName,tempDir);

            using (Process process = new Process())

            {

                process.StartInfo.FileName = "expand";

                process.StartInfo.WorkingDirectory = this.TargetDirectory;

                process.StartInfo.Arguments = cmdString;

                process.StartInfo.RedirectStandardInput = true;

                process.StartInfo.RedirectStandardOutput = true;

                process.StartInfo.RedirectStandardError = true;

                process.StartInfo.UseShellExecute = false;

                process.Start();

                process.WaitForExit();

                //this.Response.Write(process.StandardOutput.ReadToEnd());

            }

            System.IO.DirectoryInfo tempDirectory = new System.IO.DirectoryInfo(System.IO.Path.Combine(this.TargetDirectory, tempDir));

            sbString.Append("使用CMD命令进行解压:已经解压的文件:<br />");

            foreach (var item in tempDirectory.GetFiles())

                sbString.AppendFormat("{0} <br />", item.Name);

            this.Response.Write(sbString.ToString());

        }

        #endregion

        #region 使用shell解压

        /// <summary>

        /// 使用Shell解压

        /// </summary>

        private void ExtractByShell()

        {

            //shell能解压zip和cab文件,xsn文件是cab格式文件,但是需要注意直接使用后缀xsn解压会失败。此时需要重命名为cab即可

            //shell是支持要解压的文件和目标目录相同。

            //1.重命名

            String tempString=Path.Combine(this.TargetDirectory,this.CabFileName);

            if (File.Exists(tempString)) File.Delete(tempString);

            new FileInfo(Path.Combine(this.TargetDirectory, this.XSNFileName)).CopyTo(tempString);

            //2.解压

            Shell32.ShellClass shellClass = new Shell32.ShellClass();

            Shell32.Folder sourceFoloder = shellClass.NameSpace(Path.Combine(this.TargetDirectory, this.CabFileName));

            tempString = Path.Combine(this.TargetDirectory, Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempString);

            Shell32.Folder targetDir = shellClass.NameSpace(tempString);

            foreach (var item in sourceFoloder.Items())

                targetDir.CopyHere(item, 4);

            //各个参数的含义,参照:http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/

            DirectoryInfo tempDire = new DirectoryInfo(tempString);

            System.Text.StringBuilder sbString = new System.Text.StringBuilder();

            sbString.Append("<br /><br /><hr />使用Shell32进行解压。已经解压的文件:<br />");

            foreach (var item in tempDire.GetFiles())

                sbString.AppendFormat("{0} <br />", item.Name);

            this.Response.Write(sbString.ToString());

        }

        #endregion 

    }

  最终测试结果如下:

使用CMD命令进行解压:已经解压的文件:
manifest.xsf
sampledata.xml
schema.xsd
template.xml
view1.xsl 使用Shell32进行解压。已经解压的文件:
manifest.xsf
sampledata.xml
schema.xsd
template.xml
view1.xsl

  

  在出问题的项目服务器上,使用shell32的方式进行xsn文件解压,测试后发现没有任何问题,即使高频率重复刷新。

  以上只是项目中遇到的实际情况阐述,并不一定是最好的解决方案,如果大家更好的方案,请留言。

C#使用Expand、Shell32解压Cab、XSN文件的更多相关文章

  1. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  2. [Linux] 解压tar.gz文件,解压部分文件

    遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了   数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...

  3. Linux中下载、解压、安装文件

    一.将解压包发送到linux服务器上: 1.在windos上下载好压缩包文件后,通过winscp等SFTP客户端传送给linux 2.在linux中通过wget命令直接下载 #wget [选项] [下 ...

  4. linux 无法解压过大文件解决

    [root@vmbbak yum]# unzip RHEL_5.7\ x86_64\ DVD-1.zip error: Zip file too big (greater than 429495910 ...

  5. C# 上传RAR文件 解压 获取解压后的文件名称

    此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...

  6. C#利用SharpZipLib解压或压缩文件夹实例操作

    最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享. 这里主要解决文件夹包含文件夹的解压缩问题. )下载SharpZipLib.dll,在http://www.icsharpcode. ...

  7. C# 解压RAR压缩文件

    此方法适用于C盘windows文件夹中有WinRAR.exe文件 /// 解压文件(不带密码) RAR压缩程序 返回解压出来的文件数量 /// </summary> /// <par ...

  8. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  9. [转]Ubuntu Linux 安装 .7z 解压和压缩文件

    [转]Ubuntu Linux 安装 .7z 解压和压缩文件 http://blog.csdn.net/zqlovlg/article/details/8033456 安装方法: sudo apt-g ...

随机推荐

  1. centos 6.5关闭NetworkManager

    jrhmpt01:/root# rpm -qa | grep -i network NetworkManager-glib-0.8.1-99.el6.x86_64 system-config-netw ...

  2. The Same Game(模拟)

    http://poj.org/problem?id=1027 题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有 ...

  3. 数据结构(堆):SCOI 2009 生日礼物

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩 珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个 ...

  4. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  5. JQuery固定表头插件fixedtableheader源码注释

    在开发XX车站信息系统时,需要将大量数据显示在一个巨大的表格内部,由于表格是一个整体,无法分页,加之数据很多,超出一屏,为了方便用户,决定使用固定表头的插件,经过测试,发现JQuery 插件:fixe ...

  6. poj2007

    极角排序,其实是叉乘排序 #include <iostream> #include <algorithm> #include <math.h> #include & ...

  7. OPENCV第一篇

    了解过之前老版本OpenCV的童鞋们都应该清楚,对于OpenCV1.0时代的基于 C 语言接口而建的图像存储格式IplImage*,如果在退出前忘记release掉的话,就会照成内存泄露.而且用起来超 ...

  8. Java注解的简单了解

    部分信息来自<Thinking In Java> 注解也成为元数据.什么是元数据?就是“关于数据的数据” 注解为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便 ...

  9. 再回首,Java温故知新(八):Java基础之字符串

    字符串是Java中使用频率最高的类,但是它却不属于基本类型,而是预定义了String类来表示.从String类的源码可以看到,String是基于char[]实现的,而且Java中的String是不可变 ...

  10. google(转帖)

    本帖最后由 qiushui_007 于 2014-6-10 16:14 编辑 IP Addresses of Google Global Cachewww.kookle.co.nr Bulgaria  ...