简体   繁体   English

无法在Android中使用xamarin.forms C#创建Txt文件

[英]Can't Create Txt file with xamarin.forms c# in Android

So I'm trying to create a txt file for every new day using standard c#. 因此,我尝试使用标准c#为每一天创建一个txt文件。 I don't know if I need permission from the OS or something. 我不知道我是否需要操作系统的许可。 I set you breakpoints it the code goes as if it did create the txt file but when I check the storage in the device it's nowhere to be found. 我为您设置了断点,它的代码就像创建了txt文件一样,但是当我检查设备中的存储时,找不到了。 This is my code: 这是我的代码:

using System;
using System.IO;
using System.Collections.Generic;

namespace TacosSales.Helpers
{
    public class RegisterSale
    {
        public void DailySale(int sale, int sodas, int tacos)
        {
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var filename = System.IO.Path.Combine(documents, $"ventas_{DateTime.Today.ToString("yy-MMM-dd")}.txt");
            string time = DateTime.Now.ToString("t");
            if (!File.Exists(filename))
            {
                File.WriteAllText(filename, $"{time}\n--------------\nVenta: {sale}\nTacos: {tacos}\nSodas: {sodas}\n");

            }
            else
            {
                using (StreamWriter file = new StreamWriter(myFile, true))
                {
                    file.WriteLine($"{time}: {sale}\nTacos: {tacos}\nSodas: {sodas}\n");
                }
            }

        }

Any guidance will be appreciated. 任何指导将不胜感激。

I recommend using Isolated Storage. 我建议使用隔离存储。 It is a library that puts a layer over all the platforms and will store data in the device's local storage in an isolated way so that only your app can access it. 它是一个在所有平台上放置一层的库,它将以隔离的方式将数据存储在设备的本地存储中,以便只有您的应用程序才能访问它。

https://developer.xamarin.com/api/namespace/System.IO.IsolatedStorage/ https://developer.xamarin.com/api/namespace/System.IO.IsolatedStorage/

It's similar to using normal C# file access but you don't have to worry about the device's full path as that's not relevant to you unless you need to expose the file to other applications. 这与使用常规C#文件访问类似,但是您不必担心设备的完整路径,因为与您无关,除非您需要将文件公开给其他应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM