我可以从 txt 文件内部存储(文档文件夹)读取和写入,但无法浏览到该位置,因为它不存在

本文关键字:浏览 不存在 因为 位置 内部 存储 文件 txt 文档 读取 文件夹 | 更新日期: 2023-09-27 18:36:03

好吧,所以我设法在 Xamarin Studio 中以编程方式读取/写入文件。它正在我的设备上工作。

但是,当我将文件写入的确切路径输出到控制台时,该路径甚至不存在整个手机中的任何位置!!!

这是怎么回事?

using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace ToolbarSample
{
    [Activity(Label = "ToolbarSample", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            string content = "Jason rules";
            string filename = "file.txt";
            var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.button);
            TextView viewer = FindViewById<TextView>(Resource.Id.textView1);
            if (File.Exists(documents + @"/" + filename))
            {
                string newContent = File.ReadAllText(documents + @"/" + filename);
                if (viewer != null)
                {
                    viewer.Text = newContent;
                    Console.WriteLine("File exists in: " + documents + @"/" + filename);
                }
            }
            if (button != null)
            {
                button.Click += delegate
                {
                        button.Enabled = false;
                        if (!Directory.Exists(documents))
                        {
                            viewer.Text = "Directory not found: " + documents;
                        }
                        else
                        {
                            Console.WriteLine("Directory exists.");
                            File.WriteAllText(documents + @"/" + filename, content);
                            if (!File.Exists(documents + @"/" + filename))
                            {
                                viewer.Text = "File not found: " + documents + @"/" + filename;
                            }
                            else
                            {
                                string newContent = File.ReadAllText(documents + @"/" + filename);
                                if (viewer != null)
                                {
                                    viewer.Text = newContent;
                                    Console.WriteLine("File exists in: " + documents + @"/" + filename);
                                }
                            }
                        }
                };
            }
        }
    }
}

从内部SD卡成功读取后,以下内容将输出到控制台:

目录存在。文件存在于: /data/data/ToolbarSample.ToolbarSample/files/file.txt

但是使用(许多不同的)文件管理器 - 所有具有root访问权限 - 并显示隐藏文件 - 我无法导航到该路径,因为它不存在。我什至用电话搜索了"file.txt",但没有出现一个结果。然而,每当我打开我的应用程序并单击按钮时,我都可以读取该文件。

我可以从 txt 文件内部存储(文档文件夹)读取和写入,但无法浏览到该位置,因为它不存在

指定位置的文件确实存在。您无法通过 USB 和文件资源管理器从 PC 访问该位置,但如果您使用良好的文件管理器应用程序(如根资源管理器),则可以访问该位置(和文件)。

如果您真的希望您的用户能够访问这些保存的文件,我建议您将这些文件保存到更好的位置,以便用户可以通过USB轻松地将文件从手机传输到计算机。

从文件读取/写入数据都非常简单。

public String ReadFileData()
    {
        var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        var filename = Path.Combine(path.ToString(), "loginSystem.txt");
        String line;
        objData = new List<UsersData>();
        // Read the file and display it line by line.
        StreamReader file = new StreamReader(filename);
        while ((line = file.ReadLine()) != null)
        {
            string[] words = line.Split(',');
            if (words.Length != 1)
                objData.Add(new UsersData(words[0], words[1], words[2]));
        }
        file.Close();
        return String.Empty;
    }

将数据保存到文件中

 private string SaveDataToSd(String FirstName, String Address, String Password)
    {
        var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        var filename = Path.Combine(path.ToString(), "loginSystem.txt");
        String contents = FirstName + "," + Password + "," + Address;
        try
        {
            using (StreamWriter data_file = new StreamWriter(filename, true))
            {
                data_file.WriteLine(contents);
            }
            return contents;
        }
        catch (Exception ex)
        {
            RunOnUiThread(() =>
            {
                var builder = new AlertDialog.Builder(this);
                builder.SetMessage(ex.InnerException + "Saving file went wrong");
                builder.SetTitle("Unable to save file");
                builder.Show();
            });
            return String.Empty;
        }
    }