我以这种方式写文件数据。
我多次调出这个功能。
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);
}
编译器跳过StorageFile。 为什么dataFolder和local的值为NULL?
答案 0 :(得分:0)
尝试使用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);
}