C # Use Expand and Shell32 to decompress the Cab and XSN files,
Preface:
To decompress the xsn file of the InfoPath form, the Expand command line is used in the project before unzipping. There is no problem, and an error is reported in the recent project, through analysis and decompression, we can conclude that:
1. Under normal circumstances, the expand command line decompression is normal, the same site, the same request, random decompression failure error. And the most likely case is: high-frequency page refresh.
2. Monitor the target directory to be decompressed. The directory remains unchanged when decompression fails. The directory monitoring is normal when the decompression is successful.
Then, place the expand command into the bat file. In the bat file, run the "md" command to create a random directory before executing the expand command, and run the C # code to execute the bat command, it is found that, even if the execution of the bat command is complete, directory monitoring does not find the directory created by the md command. You can only guess that C # will not be synchronized in some cases when executing the command line.
There is no time to study this synchronization problem. The project uses C # To call the COM component, and then searches the Internet for information about the cab file decompressed by the COM component, it is found that using shell32 for decompression is no problem. You only need to note the Shell32 reference method:
1. Add "Microsoft Shell Controls And Automation" reference, as shown in:
2. Generate a project. The "Interop. Shell32.dll" assembly will be generated under the bin directory, copied to other directories, and then removed the reference to Sell32:
3. Add a reference to the "Interop. Shell32.dll" assembly, and then the effect is shown in:
The reason for the above operation is: directly add the "Microsoft Shell... after the code is generated, it may not be normally called by other systems. For example, if the code generated by Win 2003 cannot be used on win2007, but after being referenced by the above method, it will work. In this way, you can use Shell to perform operations normally. For information on Shell operations can refer to: http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/
The final code is as follows: The Code also includes the cmd Command Line Method for your reference.
Code:
Public partial class Extract: System. web. UI. page {// <summary> /// name of the file to be decompressed /// </summary> private String XSNFileName = @ "infopath. xsn "; // <summary> /// extract it .... private String TargetDirectory = @ "C: \ xsn "; /// <summary> /// cab file name /// </summary> private String CabFileName = "cab. cab "; protected void Page_Load (object sender, EventArgs e) {// use the cmd command to decompress this. extractByCmd ();/ /Use shell32 to decompress this. extractByShell () ;}# unzip the region cmd command /// <summary> /// use the cmd command to decompress the package /// </summary> private void ExtractByCmd () {// use the cmd command: expand sourcefile targetDir-F: * // note the preceding command: The target directory cannot be the sourceFile directory. 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 destination String = String. format ("\" {0} \ "\" {1} \ "-F: *", this. XSNFileName, tempDir); using (Process process = new Process () {process. startInfo. fileName = "expand"; process. startInfo. workingDire Ctory = this. targetDirectory; process. startInfo. arguments = parameter string; 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 ("decompress using the CMD command: decompressed files: <br/>"); foreach (var item in tempDirectory. getFiles () sbString. appendFormat ("{0} <br/>", item. name); this. response. write (sbString. toString () ;}# endregion # use shell to decompress region /// <summary> /// use Shell to decompress it /// </summary> private void ExtractByShell () {// shell can decompress the zip and cab files. The xsn file is File in the cab format. However, you must note that using the extension xsn directly fails to decompress the file. In this case, you need to rename it cab. // shell supports the same files to be decompressed as the target directory. // 1. rename 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. decompress Shell32.ShellClass shellClass = new Shell32.ShellClass (); Shell32.Folder sourceFoloder = shellClass. nameSpace (Path. combine (this. targetDirectory, this. cabFileName); tempStri Ng = 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); // meaning of each parameter, refer to: http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/ DirectoryInfo tempDire = new DirectoryInfo (tempString); System. text. stringBuil Der sbString = new System. Text. StringBuilder (); sbString. Append ("<br/>
The final test result is as follows:
Run the CMD command to decompress the package: manifest. xsf sampledata. xml schema. xsd template. xml view1.xsl. Use Shell32 to decompress the package. Decompressed file: manifest. xsf sampledata. xml schema. xsd template. xml view1.xsl
On the problematic project server, decompress the xsn file using shell32. After the test, no problem is found, even if the refresh is performed frequently.
The above is only an explanation of the actual situation encountered in the project. It is not necessarily the best solution. If you have a better solution, leave a message.