2014-04-29 136 views
0

我試圖寫入我的項目文件夾中的txt文件,但我不能拋出異常,因爲某些原因,我嘗試過所有的東西,但它仍然不會工作無法寫入文本文件

// settings 
    // same as (ms-appx:///MyFolder/MyFile.txt) 
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation; 
    _Folder = await _Folder.GetFolderAsync("MyFolder"); 

    // acquire file 
    var _File = await _Folder.GetFileAsync("MyFile.txt"); 
    Assert.IsNotNull(_File, "Acquire File"); 

    // write content 
    var _WriteThis = "Hello World"; 
    // Error here 
    await Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis); 

這裏是我來我就一直使用錯誤發生時,我想寫入文件

下面的代碼是例外

System.UnauthorizedAccessException was unhandled 
    HResult=-2147024891 
    Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) 
    Source=mscorlib 
    StackTrace: 
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
     at ManipulatingTxtFiles.MainPage.<addBtn_Click>d__a.MoveNext() in c:\Users\Tunde\Documents\Visual Studio 2012\Projects\ManipulatingTxtFiles\ManipulatingTxtFiles\MainPage.xaml.cs:line 115 
    --- End of stack trace from previous location where exception was thrown --- 
     at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state) 
     at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore() 
    --- End of stack trace from previous location where exception was thrown --- 
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
     at System.Threading.WinRTSynchronizationContext.Invoker.<InvokeCore>b__0(Object o) 
     at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
     at System.Threading.ThreadPoolWorkQueue.Dispatch() 
     at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 
    InnerException: 
+4

你能發佈異常消息和堆棧跟蹤嗎? –

+0

它已被添加 – user3063540

+0

爲什麼不直接使用[StreamWriter](http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter)對象? – Brandon

回答

3

你的包文件夾及其所有內容(即任何被引用的東西m Windows.ApplicationModel.Package.Current.InstalledLocation,ms-appx:///或ms-appx-web:///是隻讀的。因此,訪問拒絕異常。

改爲使用您的Windows.Storage.ApplicationData.LocalFolder或TemporaryFolder(請參閱http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.applicationdata.aspx),這也可以使用ms-appdata:/// local /或ms-appdata:/// temp /來完成。這些位置是可讀寫的。

+0

因此,您無法編輯或讀取直接在您的解決方案中的文件 – user3063540

+1

您可以閱讀,但無法書寫。簡而言之,您不能修改應用程序包作爲從商店下載的用戶,這對於其他安全原因很重要。 –