这是一个用于android的xamarin应用程序,用于压缩或提取用户在文本框中输入的给定文件。我将示例文件放在assets文件夹中。单击按钮时的函数zip应该压缩文件在输入文件名并单击按钮时,即使提供了文件的路径,它也会抛出一个文件未发现的异常。
namespace zipfile
{
[Activity (Label = "zipfile", MainLauncher = true)]
public class MainActivity : Activity
{
string t;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button> (Resource.Id.button1);
button1.Click += delegate {
EditText text = FindViewById<EditText>
(Resource.Id.editText2);
if (null == text)
return;
t="\\zip\\zipfile\\Assets\\"+ text.Text;
//Toast.MakeText(this,"file
zipped",ToastLength.Long).Show();
ZipOutputStream.Zip(t, t, 128);
};
Button button2 = FindViewById<Button> (Resource.Id.button2);
button2.Click += delegate {
//Toast.MakeText(this,"file
unzipped",ToastLength.Long).Show();
ZipInputStream.UnZip (t, t, 128);
};
}
}
}
我是Xamarin的新手请建议我处理这个例外的方法。
答案 0 :(得分:0)
您无法写入APK内的路径。
一个简单的文件IO示例:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "file.txt");
using (var file = File.Open(filePath, FileMode.Create, FileAccess.Write))
using (var strm = new StreamWriter(file))
{
strm.Write(data);
}