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

I can read and write from a txt file internal storage (documents folder) but cannot browse to that location because it doesn't exist

好吧,我已经设法在 Xamarin Studio 的 C# 中以编程方式 read/write 文件。它在我的设备上运行。

但是,当我将写入文件的确切路径输出到控制台时,该路径在整个 phone!!!!

中根本不存在

怎么样?

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

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

Directory exists. File exists in: /data/data/ToolbarSample.ToolbarSample/files/file.txt

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

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

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

It quite simple both Read/Write data from File .

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

Save data into file

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