---
title: "C# 如何读取rar压缩包里面的文件信息"
author: "dnanhui"
date: "2013-01-22T17:08:16Z"
tags: []
categories: ["编程语言"]
slug: "c-rar"
draft: false
lastmod: "2013-01-22T17:08:16Z"
id: 1773871
url: "https://dnanhui.iteye.com/blog/1773871"
---

private string RunCmd(string command) { //实例一个Process类，启动一个独立进程 Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c "+"\\""+System.Windows.Forms.Application.StartupPath+"\\\\r ar.exe" +"\\""+ command; //设定程式执行参数 p.StartInfo.UseShellExecute = false; //关闭Shell的使用 p.StartInfo.RedirectStandardInput = true; //重定向标准输入 p.StartInfo.RedirectStandardOutput = true; //重定向标准输出 p.StartInfo.RedirectStandardError = true; //重定向错误输出 p.StartInfo.CreateNoWindow = true; //设置不显示窗口 p.Start(); //启动 string retstr=p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); return retstr; //从输出流取得命令执行结果 } /// /// 获得RAR信息 /// /// 包文件路径 /// private Hashtable CheckRar(string rar\_path) { string strcmd1 = string.Format(" lb {0} ", rar\_path); string outcmd\_string1 = RunCmd(strcmd1).Replace("\\r\\n", "|"); outcmd\_string1 = outcmd\_string1.Remove(outcmd\_string1.LastIndexOf('|')); string strcmd2 = string.Format(" l {0} ", rar\_path); string outcmd\_string2 = RunCmd(strcmd2); string\[] strfilenames = outcmd\_string1.Split('|'); int filecount = strfilenames.Length; string\[] strfilesizes = new string\[filecount]; for (int i = 0; i &lt; filecount; i++) { string filesize = outcmd\_string2.Substring(outcmd\_string2.IndexOf(strfilenames\[i]) + strfilenames\[i].Length).Trim(); filesize = filesize.Substring(0, filesize.IndexOf(" ")); strfilesizes\[i] = filesize; } Hashtable ht\_rar = new Hashtable(); for (int i = 0; i &lt; filecount; i++) { ht\_rar.Add(strfilenames\[i],strfilesizes\[i]); } return ht\_rar; }