Search code examples
uwpfilesystemswindows-iot-core-10

UWP application crashes on file write


I try to write to file with UWP application running on Windows IoT 10 Core, but application closes unexpectedly.

The code:

private async void logFile()
{
    StorageFile file = await KnownFolders.DocumentsLibrary.CreateFileAsync("robodem.log", CreationCollisionOption.OpenIfExists);
    using (Stream fileStream = await file.OpenStreamForWriteAsync())
    using (var streamWriter = new StreamWriter(fileStream))
    {
        streamWriter.Write("test");
    }
}

I added permissions to Package.appxmanifest:

<Capabilities>
    <Capability Name="internetClient" />
    <uap:Capability Name="musicLibrary" />
    <uap:Capability Name="removableStorage" />
    <uap:Capability Name="picturesLibrary" />
    <uap:Capability Name="videosLibrary" />
    <uap:Capability Name="documentsLibrary" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="serialcommunication">
        <Device Id="any">
            <Function Type="name:serialPort" />
        </Device>
    </DeviceCapability>
</Capabilities>

When I reach the code, application just shutting down, and default Hello World application is appear.

My questions:

  • Where to find application log on Windows IoT Core 10 and how to read it?
  • Where to find information about Windows IoT -> UWP filesystem abstraction?
  • Where StreamWriter("log.log") will create the log file?
  • What can be a problem?

UPDATE

I modified the code little bit as it provided here:

StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary);
try
{
    StorageFile file = await storageFolder.CreateFileAsync("robodem.log", CreationCollisionOption.ReplaceExisting);

    using (Stream fileStream = await file.OpenStreamForWriteAsync())
    using (var streamWriter = new StreamWriter(fileStream))
    {
        streamWriter.Write("test");
    }
}
catch (Exception ex)
{
    onMessageOccured(Severity.Error, ex.Message);
}

The error I see:

enter image description here

UPDATE

I put debugger type to mixed mode, it doesn't helps.


Solution

  • In general using the documents library is neither recommended or permitted. Please see this blog about documentsLibrary capability in UWP applications. It's better to change the location to save. If you still want to use the DocumentsLibrary,please add File Type Associations to your app manifest that declare specific file types that your app can access.

    enter image description here