简体   繁体   中英

FileIO System.UnauthorizedAccessException error

I am trying to write to a .txt file through my Windows 8.1 UA. My C# code looks like this:

var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");

And I am getting the System.UnauthorizedAccessException error when WriteTextAsync executes. I tried elevating my program and still same problem.

Checked all processes in case the .txt file is open somewhere, nothing.

File is not Read-only.

sampleFile.Path returns me a valid path C:\\Users\\myname\\Documents\\Visual Studio 2015\\Projects\\App2\\App2\\bin\\Debug\\AppX\\info.txt

Any ideas what could be going wrong here? Sorry if this is a newbie question.

Edit: Adding error message Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll

Stacktrace

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()

Adding GetFreeSignal function

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;
                    }
                }
            }
        }

This issue is caused by the sandboxing built into UA/UWP development. By default, applications are not allowed unrestricted access to the file system.

The easiest solution is to use the User's Local StorageFolder.

Here are other solutions: https://msdn.microsoft.com/en-us/windows/uwp/files/file-access-permissions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM