在Windows IoT 10 Core上使用UWP创建物理文件
我尝试在具有Windows IoT 10 Core的设备操作系统中创建一些物理文件,使用以下代码:在Windows IoT 10 Core上使用UWP创建物理文件,uwp,windows-10-iot-core,Uwp,Windows 10 Iot Core,我尝试在具有Windows IoT 10 Core的设备操作系统中创建一些物理文件,使用以下代码: StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary); try { StorageFile file = await storageFolder.CreateFileAsync("robodem.log", CreationCo
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”的访问被拒绝
这是包的配置。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
获取文件路径。当您通过VisualStudio部署UWP应用程序时,它将使用DefaultAccount用户。因此,文件路径是C:\Data\Users\DefaultAccount\Pictures\robodem.log
System.Diagnostics.Debug.WriteLine(file.Path);
并使用-File
而不是-Directory
,然后删除-ErrorAction SilentlyContinue
。因此,请尝试以下命令:getchilditem-Path c:\-Include robodem.log-file-Recurse
对路径“c:\robodem.log”的访问被拒绝
并非设备上的所有文件夹都可以被通用Windows应用程序访问。请参阅。记住,UWP应用程序也可以使用System.IO,因此,如果您还没有使用过System.IO,请尝试那里的一些文件操作功能