win10 UWP读写文件

时间:2022-11-21 22:11:35

C# uwp应用的文件读写最常见错误就是没有权限。

而最简单的方法是对已知的文件路径进行访问

已知的文件路径常见的是自身的路径

权限这个和之前不同,UWP读写文件多用StorageFile来读写文件

[TOC]

StorageFile没有直接的打开一个文件的做法,而是通过StorageFolder创建,打开文件来进行。

下面代码需要using Windows.Storage;

获取应用目录的文件夹

StorageFolder folder;
            folder = ApplicationData.Current.LocalFolder;

创建文件

获取后,这里不需要权限,可以直接创建文件

folder.CreateFileAsync(file_name , CreationCollisionOption.ReplaceExisting);

其中ApplicationData.CurrentCurrent的值可以是 LocalCacheFolder 本地临时文件夹,LocalFolder 本地文件夹,LocalSettings 本地设置,RoamingFolder 漫游文件夹,RoamingSettings 漫游设置

创建文件的时候可以选择,如果存在了文件的选择CreationCollisionOption里ReplaceExisting 如果文件或文件夹已存在,则替换现有的项

FailIfExists 默认值 如果文件或文件夹已存在,则引发类型为 System.Exception 的异常。

还有用的比较少的选项。

创建文件夹

创建文件夹和创建文件也是一样

先获取本地的文件夹StorageFolder folder;

folder = ApplicationData.Current.LocalFolder;


创建文件夹

 folder.CreateFolderAsync(folder_name,CreationCollisionOption.ReplaceExisting);    

为了显示已经做的,我做了一个函数来显示void notify(string str)

把做出来的东西显示出来

读写文件

把创建的文件存为file_demonstration,对file_demonstration读写

可以把这个新的东西变为熟悉的

需要using System.IO;

Stream file = await file_demonstration.OpenStreamForWriteAsync()

这样读写就很简单

写文件代码

            using (Stream file = await file_demonstration.OpenStreamForWriteAsync())
            {
                using (StreamWriter write = new StreamWriter(file))
                {
                    write.Write("写入");
                }
            }

读文件

            string s;
            using (Stream file = await file_demonstration.OpenStreamForReadAsync())
            {
                using (StreamReader read = new StreamReader(file))
                {
                    s = read.ReadToEnd();
                }
            }

微软官方的读写文件

using Windows.Storage.Streams;

写文件

            using (StorageStreamTransaction transaction = await file_demonstration.OpenTransactedWriteAsync())
            {
                using (DataWriter dataWriter = new DataWriter(transaction.Stream))
                {
                    dataWriter.WriteString(str);
                    transaction.Stream.Size = await dataWriter.StoreAsync();
                    await transaction.CommitAsync();
                }
            }

读文件

using (IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read))
            {
                using (DataReader dataReader = new DataReader(readStream))
                {
                    UInt64 size = readStream.Size;
                    if (size <= UInt32.MaxValue)
                    {
                        UInt32 numBytesLoaded = await dataReader.LoadAsync((UInt32)size);
                        string fileContent = dataReader.ReadString(numBytesLoaded);
                    }
                }
            }

别的文件夹

我们用软件安装目录的时候还是比较少,我们需要用到的文件夹还有很多,现在下面就有一些系统文件夹可以使用

图片库

选图片库可以使用folder_demonstration = KnownFolders.PicturesLibrary;

在生成前在清单请求权限

win10 UWP读写文件

win10 UWP读写文件

除了图片库还有很多,在KnownFolders可以看到,使用前需要请求权限。

还有下载文件DownloadsFolder

folder_demonstration = DownloadsFolder.CreateFolderAsync(folder_name);

选取文件

系统给的我们有时候还需要用户选的文件

选取文件需要Windows.Storage.Pickers.FileOpenPicker,使用picker.PickSingleFileAsync()来出现选择界面

在使用picker.PickSingleFileAsync()要设置后缀,选择文件的后缀picker.FileTypeFilter.Add(".txt");

一般我们有推荐第一次使用选择进入的文件夹picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;

选取文件夹和选取文件一样

Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            folderPicker.FileTypeFilter.Add(".txt");
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                notify("选择" + folder.Name);
                folder_demonstration = folder;
            }

全部代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace file
{
    public class model
    {
        public model()
        {
            //C# uwp应用的文件读写最常见错误
            //就是没有权限。
            //而最简单的方法是对已知的文件
            //路径进行访问
            //自身的路径
            ce();

        }
        /// <summary>
        /// 结果
        /// </summary>
        public string result
        {
            set
            {
                _result.Add(value);
            }
            get
            {
                StringBuilder t = new StringBuilder();
                foreach (string s in _result)
                {
                    t.Append(s);
                }
                return t.ToString();
            }
        }
        public async void ce()
        {
            //创建文件
            await create_ApplicationData("测试.txt");
            //创建文件夹
            await folder_ApplicationData("在安装的路径创建不需要权限");

            //读写文件和以前一样
            await write_ApplicationData();
            await read_ApplicationData();

            //如果要读写别的地方的文件,可以用
            //folder_demonstration = KnownFolders.PicturesLibrary;
            //要在清单请求权限
            //DownloadsFolder 下载文件夹这里也可以
            //folder_demonstration = DownloadsFolder.CreateFolderAsync(folder_name);
            //也可以让用户选

        }
        private StorageFile file_demonstration;//UWP 采用StorageFile来读写文件
        private StorageFolder folder_demonstration;//folder来读写文件夹
        /// <summary>
        /// 自身路径创建文件
        /// </summary>
        /// <param name="file_name">要创建文件名字</param>
        /// <returns>已经创建的文件</returns>
        public async Task create_ApplicationData(string file_name)
        {
            StorageFolder folder;
            folder = ApplicationData.Current.LocalFolder;
            //Current的值可以是
            //LocalCacheFolder 本地临时文件夹
            //LocalFolder 本地文件夹
            //LocalSettings 本地设置
            //RoamingFolder 漫游文件夹
            //RoamingSettings 漫游设置

            file_demonstration = await folder.CreateFileAsync(file_name , CreationCollisionOption.ReplaceExisting);
            //CreationCollisionOption 可以选择
            //     如果文件或文件夹已存在,则自动为指定名称的基础追加一个编号。例如,如果 MyFile.txt 已存在,则新文件名为 MyFile (2).txt。如果 MyFolder
            //     已存在,则新文件夹名为 MyFolder (2)。
            //GenerateUniqueName
            //ReplaceExisting 如果文件或文件夹已存在,则替换现有的项。
            //FailIfExists 默认值 如果文件或文件夹已存在,则引发类型为 System.Exception 的异常。
            //OpenIfExists 如果文件或文件夹已存在,则返回现有的项。

            notify("创建文件成功,文件路径" + file_demonstration.Path);
        }

        /// <summary>
        /// 自身路径创建文件夹
        /// </summary>
        /// <param name="file_name">要创建文件夹名字</param>
        /// <returns>已经创建的文件夹</returns>
        public async Task folder_ApplicationData(string folder_name)
        {
            StorageFolder folder;
            folder = ApplicationData.Current.LocalFolder;
            folder_demonstration = await folder.CreateFolderAsync(folder_name , CreationCollisionOption.ReplaceExisting);
            notify("创建文件夹成功,文件夹路径" + folder_demonstration.Path);
        }

        /// <summary>
        /// 对自身路径进行文件读写
        /// </summary>
        public async Task write_ApplicationData()
        {
            using (Stream file = await file_demonstration.OpenStreamForWriteAsync())
            {
                using (StreamWriter write = new StreamWriter(file))
                {
                    write.Write("写入");
                }
            }

            //using (StorageStreamTransaction transaction = await file_demonstration.OpenTransactedWriteAsync())
            //{
            //    using (DataWriter dataWriter = new DataWriter(transaction.Stream))
            //    {
            //        dataWriter.WriteString(str);
            //        transaction.Stream.Size = await dataWriter.StoreAsync();
            //        await transaction.CommitAsync();
            //    }
            //}

            notify("写入文件成功");
        }
        /// <summary>
        /// 读文件
        /// </summary>
        public async Task read_ApplicationData()
        {
            string s;
            using (Stream file = await file_demonstration.OpenStreamForReadAsync())
            {
                using (StreamReader read = new StreamReader(file))
                {
                    s = read.ReadToEnd();
                }
            }
            notify("读文件\"" + s + "\"");
        }
        public async Task file_open()
        {
            Windows.Storage.Pickers.FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
            //显示方式
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            //选择最先的位置
            picker.SuggestedStartLocation =
                Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            //后缀名
            picker.FileTypeFilter.Add(".txt");

            StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                notify("选择 " + file.Name);
                file_demonstration = file;
            }
        }
        public async Task folder_open()
        {
            Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            folderPicker.FileTypeFilter.Add(".txt");
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                notify("选择" + folder.Name);
                folder_demonstration = folder;
            }
        }

        private List<string> _result = new List<string>();
        private void notify(string str)
        {
            _result.Add(str + "\r\n");
        }

    }
}

https://code.csdn.net/lindexi_gd/uwp_write_read_file