c#讀取apk 資訊

來源:互聯網
上載者:User

  昨天遇到一個問題,需要通過c#讀取apk包的資訊。baidu,google了一大堆東西,也沒有找到相關的資料,有的也只是通過kvm把jar轉換為.net程式集來調用,試了一下,各種不穩定,各種錯誤。

  在大概11點半的時候,終於在codeplex上面找到一個http://androidxmldotnet.codeplex.com/ 項目,可以解析AndroidManifest.xml。

     具體代碼如下

  

using AndroidXml;using Ionic.Zip;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace APKRead{    class NamespaceInfo    {        public string Prefix { get; set; }        public string Uri { get; set; }    }    class Program    {        static List<AndroidInfo> androidInfos = new List<AndroidInfo>();        static void Main(string[] args)        {            //要分析的檔案名稱            var manifest = "AndroidManifest.xml";            //讀取apk,通過解壓的方式讀取            using (var zip = ZipFile.Read("News.apk"))            {                using (Stream zipstream = zip[manifest].OpenReader())                {                    //將解壓出來的檔案儲存到一個路徑(必須這樣)                    using (var fileStream = File.Create(manifest, (int)zipstream.Length))                    {                        // Initialize the bytes array with the stream length and then fill it with data                        byte[] bytesInStream = new byte[zipstream.Length];                        zipstream.Read(bytesInStream, 0, bytesInStream.Length);                        // Use write method to write to the file specified above                        fileStream.Write(bytesInStream, 0, bytesInStream.Length);                    }                }            }            //讀取解壓檔案的位元組數            byte[] data = File.ReadAllBytes(manifest);            if (data.Length == 0)            {                throw new IOException("Empty file");            }            #region 讀取檔案內容            using (var stream = new MemoryStream(data))            {                var reader = new AndroidXmlReader(stream);                while (reader.Read())                {                    switch (reader.NodeType)                    {                        case XmlNodeType.Element:                            {                                AndroidInfo info = new AndroidInfo();                                androidInfos.Add(info);                                info.Name = reader.Name;                                info.Settings = new List<AndroidSetting>();                                for (int i = 0; i < reader.AttributeCount; i++)                                {                                    reader.MoveToAttribute(i);                                    AndroidSetting setting = new AndroidSetting() { Name = reader.Name, Value = reader.Value };                                    info.Settings.Add(setting);                                }                                reader.MoveToElement();                                break;                            }                    }                }            }            #endregion            File.Delete(manifest);            StringBuilder builder = new StringBuilder();            foreach (var androidInfo in androidInfos)            {                builder.Append(string.Format("{0}:",androidInfo.Name));                foreach (var setting in androidInfo.Settings)                {                    builder.Append("{");                    builder.Append(string.Format("'{0}':'{1}'",setting.Name,setting.Value));                    builder.Append("},");                }                builder.Append("\n\n");            }            Console.WriteLine(builder.ToString());        }    }    /// <summary>    /// android應用程式資訊    /// </summary>    public class AndroidInfo    {        public string Name { get; set; }        public List<AndroidSetting> Settings { get; set; }    }    /// <summary>    /// 設定    /// </summary>    public class AndroidSetting    {        public string Name { get; set; }        public string Value { get; set; }    }}      

  引用的Ionic.Zip庫可以直接通過 nuget下載。

  快12點的時候,終於好了,然後睡覺去了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.