文件 IO System.UnauthorizedAccessException 错误
FileIO System.UnauthorizedAccessException error
我正在尝试通过我的 Windows 8.1 UA 写入 .txt 文件。
我的 C# 代码如下所示:
var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");
并且在执行 WriteTextAsync
时出现 System.UnauthorizedAccessException
错误。
我试着提升我的程序,但仍然是同样的问题。
检查所有进程以防 .txt 文件在某处打开,但没有。
文件不是只读的。
sampleFile.Path
returns 我一个有效的路径 C:\Users\myname\Documents\Visual Studio 2015\Projects\App2\App2\bin\Debug\AppX\info.txt
知道这里可能出了什么问题吗?对不起,如果这是一个新手问题。
编辑:添加错误消息Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
堆栈跟踪
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App2.Data.SampleDataSource.<GetFreeSignal>d__11.MoveNext()
添加 GetFreeSignal 函数
public static async Task<string> GetFreeSignal()
{
string sURL = await GetLastPage();
using (HttpClient clientduplicate = new HttpClient())
{
clientduplicate.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");
using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
using (HttpContent contentduplicate = responseduplicate.Content)
{
try
{
string resultduplicate = await contentduplicate.ReadAsStringAsync();
var websiteduplicate = new HtmlDocument();
websiteduplicate.LoadHtml(resultduplicate);
MatchCollection match = Regex.Matches(resultduplicate, "\[B\](.*?)<img", RegexOptions.Singleline);
var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");
return match[match.Count - 1].Groups[1].Value.TrimStart().Replace("'", "'").Replace("&", "&").Replace(""", "\"").Replace("’", "'").Replace("<br/>", "").Replace("<i>", "").Replace("</i>", "");
}
catch (Exception ex1)
{
Debug.WriteLine(ex1.StackTrace);
return string.Empty;
//throw ex1.InnerException;
}
}
}
}
此问题是由 UA/UWP 开发中内置的沙盒引起的。默认情况下,不允许应用程序不受限制地访问文件系统。
最简单的解决方案是使用用户的本地存储文件夹。
以下是其他解决方案:
https://msdn.microsoft.com/en-us/windows/uwp/files/file-access-permissions
我正在尝试通过我的 Windows 8.1 UA 写入 .txt 文件。 我的 C# 代码如下所示:
var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");
并且在执行 WriteTextAsync
时出现 System.UnauthorizedAccessException
错误。
我试着提升我的程序,但仍然是同样的问题。
检查所有进程以防 .txt 文件在某处打开,但没有。
文件不是只读的。
sampleFile.Path
returns 我一个有效的路径 C:\Users\myname\Documents\Visual Studio 2015\Projects\App2\App2\bin\Debug\AppX\info.txt
知道这里可能出了什么问题吗?对不起,如果这是一个新手问题。
编辑:添加错误消息Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
堆栈跟踪
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at App2.Data.SampleDataSource.<GetFreeSignal>d__11.MoveNext()
添加 GetFreeSignal 函数
public static async Task<string> GetFreeSignal()
{
string sURL = await GetLastPage();
using (HttpClient clientduplicate = new HttpClient())
{
clientduplicate.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");
using (HttpResponseMessage responseduplicate = await clientduplicate.GetAsync(sURL))
using (HttpContent contentduplicate = responseduplicate.Content)
{
try
{
string resultduplicate = await contentduplicate.ReadAsStringAsync();
var websiteduplicate = new HtmlDocument();
websiteduplicate.LoadHtml(resultduplicate);
MatchCollection match = Regex.Matches(resultduplicate, "\[B\](.*?)<img", RegexOptions.Singleline);
var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");
return match[match.Count - 1].Groups[1].Value.TrimStart().Replace("'", "'").Replace("&", "&").Replace(""", "\"").Replace("’", "'").Replace("<br/>", "").Replace("<i>", "").Replace("</i>", "");
}
catch (Exception ex1)
{
Debug.WriteLine(ex1.StackTrace);
return string.Empty;
//throw ex1.InnerException;
}
}
}
}
此问题是由 UA/UWP 开发中内置的沙盒引起的。默认情况下,不允许应用程序不受限制地访问文件系统。
最简单的解决方案是使用用户的本地存储文件夹。
以下是其他解决方案: https://msdn.microsoft.com/en-us/windows/uwp/files/file-access-permissions