uwp - 在 Windows IoT 10 Core 上使用 UWP 创建物理文件
问题描述
我尝试使用以下代码在具有 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>
我如何创建一个物理文件来写入日志信息并进一步阅读?
解决方案
你的代码是正确的。
可以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 应用程序访问。请参阅文件访问权限。
推荐阅读
- apache-spark - 在 pyspark 数据框中添加行并相应地调整列序列
- c++ - 使用 setprecision (std::numeric_limits) 从 double 转换为字符串
::max_digits10) - kubernetes-ingress - 如何在 kuberenetes 入口中永久设置 nginx.conf?
- android - material-1.3.0\res\values\values.xml:3523:23:AAPT:错误:资源“attr/iconSize”与配置“”的重复值
- c# - 如何在发送前重命名文件
- ios - Disclosure Indicator 背景后面的 Swift UITableViewCell 视图在明亮模式下没有变化
- winapi - 卸载 UWP 应用时显示 toast 通知
- windows - Windows 10 中的 setx 删除了反斜杠
- python - 如何限制对角色高于他们的用户详细信息页面的访问?
- winapi - 使用来自 win32 的蓝牙 LE