【发布时间】:2019-01-18 12:21:07
【问题描述】:
我正在制作一个 UWP 应用程序,并且一直拥有 System.UnauthorizedAccessException
这是我的主文件的完整代码
private void SaveButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var user = new Configuration();
string Email = txtEmail.Text.Trim();
user[Email]["Value"].StringValue = "test";
string dest = Path.GetFullPath(@"C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\Test\user.cfg");
user.SaveToFile(dest);
}
当我阅读 UWP 应用程序文件访问权限的文档时,它建议添加这一行
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap"> ...
这是我的 AppxManifest 文件
<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap" xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5">
---
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
所以当我尝试保存文件时。我仍然有这个错误。
System.UnauthorizedAccessException HResult=0x80070005
Message=访问路径 'C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\Test\user.cfg' 被拒绝。源 = System.Private.CoreLib StackTrace:在 System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
在 System.IO.FileStream.CreateFileOpenHandle(文件模式模式,文件共享 共享,FileOptions 选项)在 System.IO.FileStream..ctor(String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize,FileOptions 选项)在 SharpConfig.Configuration.SaveToFile(字符串文件名,编码 编码)在 SharpConfig.Configuration.SaveToFile(字符串文件名) 在 AttendanceApp.MainPage.SaveButton_Click(对象发件人, RoutedEventArgs e) 在 C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\MainPage.xaml.cs:line 57
顺便说一句,我也在使用SharpConfig 来编辑我的.cfg 文件
任何想法为什么会发生这种情况?任何帮助都会很棒
【问题讨论】:
-
您是否检查过是否可以手动创建/访问该文件?另外,我猜你是在 VS 中运行 - 你是以管理员身份运行 VS 吗?
-
@pm_2 我可以访问该文件,因为我创建了它,我会试一试
-
@pm_2 尝试以管理员身份运行VS,问题依旧
-
@newboooooo:您似乎正在尝试写入与您的 exe 相同的目录。这是不支持的。尝试另一个目录。
-
另外,您访问已知的
StorageFolder应该没有问题。即StorageFolder LocalFolder = ApplicationData.Current.LocalFolder;
标签: c# windows xaml uwp storage