C#中,上传apk时,怎样获取apk的基本信息 20
2个回答
展开全部
安卓的APK么?
安卓的APK是个压缩包格式,我给你个思路 具体代码你去写 还是比较简单的
分为两步:
①上传按钮点击时解压APK压缩包
②获取压缩包内AndroidManifest.xml(apk的包名,版本号,版本名称都在此XML文件内)
③解析这个XML
既然答了 我就完善一下:(在codeplex上面找到的)
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下载。
追问
using AndroidXml;
在哪下载啊,我下载下来是错的
var reader = new AndroidXmlReader(stream);报错
展开全部
C#通过调用aapt获取apk文件的包名等信息
http://www.sum16.com/desinger/c-sharp-read-package-name-by-aapt.html
http://www.sum16.com/desinger/c-sharp-read-package-name-by-aapt.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询