【问题标题】:File Not Found Exception In Xamarin programXamarin 程序中未找到文件异常
【发布时间】:2014-02-14 09:08:52
【问题描述】:

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

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

【问题讨论】:

    标签: c# android .net xamarin


    【解决方案1】:

    您不能写入 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2019-03-29
      • 2019-06-26
      相关资源
      最近更新 更多