【发布时间】:2019-07-12 00:32:15
【问题描述】:
我尝试使用以下代码在具有 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 应用也可以使用 System.IO,所以如果你还没有尝试过那里的一些文件操作功能