写入C#中的txt文件

时间:2015-09-16 08:02:19

标签: c# windows-phone-8

我以这种方式写文件数据。

我多次调出这个功能。

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?

1 个答案:

答案 0 :(得分:0)

尝试使用asyncawait,因为它需要是异步的。

 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);
    }