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("'", "'").Replace("&", "&").Replace(""", "\"").Replace("’", "'").Replace("<br/>", "").Replace("<i>", "").Replace("</i>", "");
}
catch (Exception ex1)
{
Debug.WriteLine(ex1.StackTrace);
return string.Empty;
//throw ex1.InnerException;
}
}
}
}