在Windows IoT 10 Core上使用UWP创建物理文件

问题描述 投票:1回答:1

我尝试使用Windows IoT 10 Core在设备操作系统中创建一些物理文件,使用以下代码:

StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);

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

    onMessageOccured(Severity.Success, "Done");
}
catch (Exception ex)
{
    onMessageOccured(Severity.Error, ex.Message);
}

但是,当我使用以下方法搜索文件时,我没有任何例外:

Get-Childitem –Path c:\ -Include robodem.log -Directory -Recurse -ErrorAction SilentlyContinue

我找不到。

此外,storageFolder.Attributes等于FileAttributes.Archive

如果我使用:

StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);

try
{
    using(FileStream stream = new FileStream("c:\\robodem.log", FileMode.OpenOrCreate))
    using (StreamWriter streamWriter = new StreamWriter(stream))
    {
        streamWriter.Write("test");
    }

    onMessageOccured(Severity.Success, "Done Base");
}
catch (Exception ex)
{
    onMessageOccured(Severity.Error, ex.Message);
}

我得到这个例外:

访问路径'c:\ robodem.log'被拒绝。

这是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>

如何创建物理文件以将日志信息写入并进一步读取?

uwp windows-10-iot-core
1个回答
2
投票

你的代码是正确的。

您可以使用file.Path来获取文件路径。通过Visual Studio部署UWP应用程序时,它使用DefaultAccount用户。所以文件路径是C:\Data\Users\DefaultAccount\Pictures\robodem.log

System.Diagnostics.Debug.WriteLine(file.Path);

并使用-File而不是-Directory并删除-ErrorAction SilentlyContinue。所以试试这个命令:Get-Childitem -Path c:\ -Include robodem.log -file -Recurse

访问路径'c:\ robodem.log'被拒绝。

并非通用Windows应用程序可以访问您设备上的所有文件夹。请参考qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.