我有一個程序,使用下面的方法將一些數據寫入文件。如何檢查寫入目錄或文件的權限?
public void ExportToFile(string filename)
{
using(FileStream fstream = new FileStream(filename,FileMode.Create))
using (TextWriter writer = new StreamWriter(fstream))
{
// try catch block for write permissions
writer.WriteLine(text);
}
}
運行程序時,我得到一個錯誤:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'mypath' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolea bFromProxy)
問:我需要趕上這一點,我怎麼授予什麼代碼的訪問?
此解決方案不起作用,SecurityManager.IsGranted即使我沒有對文件的寫權限返回true(Win XP的SP3) – 2010-01-07 13:54:56
你運行本地管理員,或作爲無權訪問的權限較低的用戶? – Josh 2010-01-08 03:58:10
SecurityManager.IsGranted歸因於已廢棄。這個答案可能不會永久工作。 – MarkPflug 2011-08-03 17:35:19