现在正在学习C#的Windows编程。在自己的做项目的过程中,经常要上网去查找答案。想在这里记录一下这个项目中的一些小技巧。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; using System.Collections; using System.Drawing; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; namespace AntlerTekSX { public class PxlFile { public string pxlFilePath; public string pxlBundleName; public string pxlBundleDisplayName; public string pxlBundleVersion; public Bitmap pxlIconImage; public string pxlInfoPlist; public PxlFile(string pxl) { if (File.Exists(pxl)) { this.pxlFilePath = pxl; } this.getInfoFromPxlFile(); this.readPlist(); } public void getInfoFromPxlFile() { ZipInputStream s = new ZipInputStream(File.OpenRead(pxlFilePath)); ZipEntry zEntry; int fileFound = 0; string currFileName; MemoryStream ms = new MemoryStream(); string plist = ""; Bitmap iconImage = null; while ((zEntry = s.GetNextEntry()) != null && fileFound < 2){ currFileName = Path.GetFileName(zEntry.Name); if (currFileName.ToLower() == "info.plist") // 找到Info.plist文件 { int size = 2048; byte[] data = new byte[2048]; // read Info.plist file to a string StringBuilder sb = new StringBuilder(); while (true){ size = s.Read(data, 0, data.Length); if (size > 0){ plist += Encoding.ASCII.GetString(data, 0, size).Trim(); }else{ fileFound++; break; } } } else if (currFileName.ToLower() == "icon.png") // 找到Icon文件 { int size = 2048; byte[] data = new byte[2048]; int currOffset = 0; ms.SetLength(zEntry.Size); while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { ms.Write(data, 0, size); currOffset += size; } else { fileFound++; iconImage = (Bitmap)Image.FromStream(ms); ms.Close(); break; } } } } // 得到plist和icon图片 this.pxlInfoPlist = this.clearUpPlist(plist); this.pxlIconImage = iconImage; } public void readPlist() { XmlDocument pListXmlDoc = new XmlDocument(); pListXmlDoc.LoadXml(this.pxlInfoPlist); XmlNode pxlInfoNode = pListXmlDoc.SelectSingleNode("plist"); XmlNode pxlDictNode = pxlInfoNode.SelectSingleNode("dict"); XmlNodeList aaa = pxlDictNode.ChildNodes; Hashtable ht = new Hashtable(); string key = ""; bool foundKey = false; foreach (XmlNode xn in aaa) { if (xn.Name == "key") { foundKey = true; key = xn.InnerText; } else { if (xn.Name == "string" && foundKey) { ht[key] = xn.InnerText; foundKey = false; } } } this.pxlBundleName = ht["CFBundleName"].ToString(); this.pxlBundleVersion = ht["CFBundleVersion"].ToString(); this.pxlBundleDisplayName = ht["CFBundleDisplayName"].ToString(); } public static string byteToString(byte[] bs) { StringBuilder sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(Convert.ToString(b)); } return sb.ToString(); } public Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } public string clearUpPlist(string plist) { int startIndex = plist.IndexOf("<?xml "); int endIndex = plist.IndexOf("</plist>"); return plist.Substring(startIndex, endIndex - startIndex + 8); } } }