Windows IoT Raspberry Pi 3 c# Create .txt file

2019-02-20 00:56发布

问题:

I am using Rasp Pi 3 with Win IoT. I am trying to create a .txt file with Data & time as file name. However, it seems that I can't create .txt file.

I am testing out by pressing a button to generate a log.txt file. After pressing the button, there is no error message. When I read the USB drive, the Log folder was created but there is not Log.txt file.

private async void Btn_Click(object sender, RoutedEventArgs e)
    {
        var removableDevices = KnownFolders.RemovableDevices;
        var externalDrives = await removableDevices.GetFoldersAsync();
        var drive0 = externalDrives[0];

        var logFolder = await drive0.CreateFolderAsync("Log");
        var logFile = await logFolder.CreateFileAsync("Log.txt");

        var byteArray = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
        using (var sourceStream = new MemoryStream(byteArray).AsRandomAccessStream())
        {
            using (var destinationStream = (await logFile.OpenAsync(FileAccessMode.ReadWrite)).GetOutputStreamAt(0))
            {
                await Windows.Storage.Streams.RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
            }
        }
    }

Thanks.

回答1:

You need to add File Type Association declarations for .txt in your app manifest like this: