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");
、いつ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
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()
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;
}
}
}
}
それが動作するかどうか – MethodMan
@MethodManが既にことを試みたあなたは手動でアプリケーションにそれを実行したときに対し、私はあなたがVS閉じることをお勧めし、管理者としてそれを再起動し、参照するためのアクセス権を持っています。 did not work –
ファイルが作成されていない可能性がありますか?プログラムを実行する前にファイルを作成してみてください。 – AxelWass