简体   繁体   中英

Windows IoT Raspberry Pi 3 c# Create .txt file

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.

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM