【问题标题】:FileIO System.UnauthorizedAccessException errorFileIO System.UnauthorizedAccessException 错误
【发布时间】:2017-02-03 19:59:06
【问题描述】:

我正在尝试通过我的 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 为我返回一个有效路径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("&apos;", "'").Replace("&amp;", "&").Replace("&quot;", "\"").Replace("&rsquo;", "'").Replace("<br/>", "").Replace("<i>", "").Replace("</i>", "");
                    }
                    catch (Exception ex1)
                    {
                        Debug.WriteLine(ex1.StackTrace);
                        return string.Empty;
                        //throw ex1.InnerException;
                    }
                }
            }
        }

【问题讨论】:

  • 您可以手动访问,而不是在应用程序中运行它,那么我建议您关闭 VS 并以管理员身份重新启动它,看看它是否有效
  • @MethodMan 已经尝试过了。没用
  • 可能是因为文件没有创建?您可以尝试在执行程序之前创建文件。
  • @AxelWass 已创建。正如我已经说过的,.Path 向我返回了 .txt 文件的有效路径
  • 我在 UWP 开发中遇到了类似的问题。您可能无权访问该文件夹,因为 UA/UWP 非常沙盒化。尝试使用用户的 AppData Storage 文件夹,看看是否有同样的访问问题。

标签: c# file exception file-handling unhandled-exception


【解决方案1】:

此问题是由 UA/UWP 开发中内置的沙盒引起的。默认情况下,不允许应用程序不受限制地访问文件系统。

最简单的解决方案是使用用户的本地存储文件夹。

以下是其他解决方案: https://msdn.microsoft.com/en-us/windows/uwp/files/file-access-permissions

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 2016-01-28
    • 2014-09-11
    相关资源
    最近更新 更多