Xamarin 程序中找不到文件异常 [英] File Not Found Exception In Xamarin program

查看:41
本文介绍了Xamarin 程序中找不到文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个用于 android 的 xamarin 应用程序,用于压缩或提取用户在文本框中输入的给定文件.我将示例文件放在资产文件夹中.按钮单击时的 zip 功能应该压缩文件在输入文件名并单击按钮时,即使提供了文件所在的路径,它也会抛出 filenotfound 异常.

This is a xamarin application for android to compress or extract a given file entered by user in the textbox. I placed the sample file in the assets folder. The function zip on button click is supposed to compress the file On entering the filename and clicking the button it is throwing a filenotfound exception even after providing path of the file located.

 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 的新手,请建议我处理这个异常的方法.

I am new To Xamarin Please suggest me way to handle this exception.

推荐答案

您不能写入 APK 内的路径.

You can't write to paths inside of the APK.

一个简单的文件 IO 示例:

A simple file IO example:

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);
}

这篇关于Xamarin 程序中找不到文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆