系统相关
首页 > 系统相关> > Windows Metro应用程序-使用C#编写和读取文本文件?

Windows Metro应用程序-使用C#编写和读取文本文件?

作者:互联网

创建文本文件:

var myFile = "test";
var folderUsed = Windows.Storage.ApplicationData.Current.LocalFolder;
var fileOption = Windows.Storage.CreationCollisionOption.ReplaceExisting;
var createdFile = await folderUsed.CreateFileAsync(myFile, fileOption);

将字符串写入创建的文本文件:

var writeThis = "write this ";
await Windows.Storage.FileIO.WriteTextAsync(createdFile, writeThis);

但是,它没有指定将在哪个部分中创建文本文件,我想在应用程序包中而不是在计算机的其他位置中创建文本文件,这可能吗?

其次,当我再次执行第二个代码时,我希望将文件写为“ write this write this”,而不是替换旧文件并创建另一个文件.

解决方法:

你应该用

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

因为其他文件夹(例如安装文件夹)受到限制,并且不允许进行读写操作. Windows应用商店应用在此区域受到很大限制.

此链接也很有用:

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758325.aspx

标签:file-io,windows-8,c
来源: https://codeday.me/bug/20191031/1976765.html