我想通過我的Windows 8.1 UA寫入.txt文件。 我的C#代碼如下所示:FileIO System.UnauthorizedAccessException錯誤
var path = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sampleFile = await path.GetFileAsync("info.txt");
await FileIO.WriteTextAsync(sampleFile, "new text");
而且我得到了System.UnauthorizedAccessException
錯誤時WriteTextAsync
執行。 我試圖提升我的程序,但仍然是同樣的問題。
檢查所有進程,以防萬一.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("'", "'").Replace("&", "&").Replace(""", "\"").Replace("’", "'").Replace("<br/>", "").Replace("<i>", "").Replace("</i>", "");
}
catch (Exception ex1)
{
Debug.WriteLine(ex1.StackTrace);
return string.Empty;
//throw ex1.InnerException;
}
}
}
}
您可以訪問手動當你在應用程序運行VS那麼我建議你關閉VS並重新啓動它爲管理員,看看它是否工作 – MethodMan
@MethodMan已經嘗試過, 。沒有工作 –
也許是因爲該文件沒有創建?您可以在執行程序之前嘗試創建該文件。 – AxelWass