微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin Android中没有此类文件或目录错误

如何解决Xamarin Android中没有此类文件或目录错误

我正在尝试在 Xamarin Android 中创建一个新的文本文件,并向该文件中写入一些字符串,但是却收到错误消息

ENOENT(没有这样的文件或目录)

下面将详细解释我的代码


   //Create a new file where we will write some texts or strings called "SaveMe.txt"
 Java.IO.File root = new Java.IO.File(Android.OS.Environment.DirectoryDocuments,"SaveMe.txt");
  //Check if file does not exist and create one 
   if(!root.Exists()){
        root.Mkdir();
    }
     //Define the content of the source file that we desire to access
       string content;
    //Read some texts from an asset file located in the Assets folder called "source.txt"
    try{
            AssetManager assets=this.Assets;
          //Define Steam reader that we will use to read the source file
       using (StreamReader streamReadr = new StreamReader(assets.Open("source.txt"))){
                    content = streamReadr.ReadLine();
            }
             //Define new File writer class that we will use to edit the target file with
            FileWriter writer = new FileWriter(root);
             //Add string from source file to target file
            writer.Append(content);
            writer.Flush();
            //Close file writer
            writer.Close();
       }catch(Java.IO.IOException m){
     //Capture the exception for debugging
       Toast.MakeText(this,m.Message,ToastLength.Long).Show(); 
       }

当我检查应用程序的目录时,我看到了Documents Directory,但是它是空的 可以成功创建该文本文件并将其写入的任何建议肯定会受到赞赏

解决方法

您为什么不使用System.IO?您将能够编写可以通过这种方式在Android和iOS之间共享的代码,并且仍然可以从共享代码编写文件。您只需提取的一部分就是从Android Assets中读取文件。

考虑以下代码:

using System;
using System.IO;

var myDocuments = System.Environment.GetFolderPath(
    System.Environment.SpecialFolder.MyDocuments);

var myFile = Path.Combine(myDocuments,"myFiles","SaveMe.txt");
var fileDirectory = Path.GetDirectoryName(myFile);
if (!Directory.Exists(fileDirectory))
    Directory.CreateDirectory(fileDirectory);

using var assetsStream = Assets.Open("source.txt");
using var fileStream = File.OpenWrite(myFile);
assetsStream.CopyTo(fileStream);

就是这样! using关键字可确保事物被冲洗并丢弃。 您也可以考虑使用CopyToAsync而不是阻止IO。

其中某些语言功能可能要求您使用LangVersion 7.3或更高版本,您可以在第一个<LangVersion>8.0</LangVersion>的csproj文件中添加<PropertyGroup>

请注意,在Android上的某些路径上,您需要请求权限才能从该路径进行写入/读取。确保阅读有关请求运行时权限的信息。

,

浏览完我的代码和该论坛上的一些帖子后,我发现您需要用反斜杠分隔字符串文件名,以便编译器知道如何将目录和文本文件逻辑结合起来。 >

//Access the root directory of Android Os
 string abso = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
//Create New Folder in root called Tutorial
 Java.IO.File newfile = new Java.IO.File(file1,filename);
//Check if folder exists and create one if false
 if (!file1.Exists())
            {
                file1.Mkdir();
            }
//Read from the stream and assign value to string variable
   try
            {
                StreamReader streamReader = new StreamReader(assets.Open("source.txt"));
                
                    content = streamReader.ReadLine();
                      all = streamReader.ReadToEnd();
                
            } catch (System.IO.IOException e)
            {
                Toast.MakeText(this,e.Message + " ",ToastLength.Long).Show();

            }
  //Define file name with folder delimiter character "/"
   string filename = "/target.txt";
   //Combine the dir and the file to create a java file
  Java.IO.File newfile = new Java.IO.File(file1,filename);
   //Check if file exists and create new one if false
     if (!newfile.Exists())
            {
                try
                {
                    newfile.CreateNewFile();
                }catch(Java.IO.IOException k)
                {
                    Toast.MakeText(this,k.Message + " ",ToastLength.Long).Show();
                }
            }
   //Try to write to the file now  
            try
            {
                FileWriter fileWriter = new FileWriter(newfile);
                fileWriter.Write(content);
                fileWriter.Append(all);
                fileWriter.Flush();
                fileWriter.Close();
            }
            catch (System.IO.IOException j)
            {
                View view = this.FindViewById<RelativeLayout>(Resource.Id.listener);
                Snackbar.Make(view,j.Message,Snackbar.LengthLong).Show();
            }

工作正常,何时可以在存储中成功找到文本文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。