[英]Write to a txt file in C#
I write to file data in this way. 我以这种方式写入文件数据。
I call out this function many times. 我多次调用此函数。
String ResultString = "data";
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(ResultString);
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await local.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);
StorageFile file = await dataFolder.CreateFileAsync("Data.txt", CreationCollisionOption.OpenIfExists);
using (var s = await file.OpenStreamForWriteAsync())
{
s.Seek(0, SeekOrigin.End);
s.Write(fileBytes, 0, fileBytes.Length);
}
The compiler skips StorageFile. 编译器将跳过StorageFile。 Why dataFolder and local have the values NULL??
为什么dataFolder和local的值为NULL?
Try using async
and await
as it needs to be asynchronous. 尝试使用
async
并await
因为它需要异步。
public static async Task<string> WriteFie()
{
string message = "abc";
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await local.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists);
StorageFile file = await dataFolder.CreateFileAsync("Data.txt", CreationCollisionOption.OpenIfExists);
await FileIO.WriteTextAsync(file, message);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.