[英]Grant WPF application access to local drive?
我正在用C#編寫一個應用程序(這對我的朋友來說非常基本),但我有一個StreamWriter對象,可以在C:中創建一個本地文件。 我必須以管理員身份運行,它運行正常,但否則崩潰“訪問路徑'C:\\ final.html'被拒絕。”
我以前從未在代碼中使用任何類型的安全性或權限。 如何在不必以管理員身份運行的情況下授予其訪問該文件夾的權限?
編輯:這是我最終做的事情:
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
using (StreamWriter output = new StreamWriter(Path.Combine(desktop, "final.html"))
{
//...additional code
output.Write(inputArray.Substring(lastIndex, range.Start - lastIndex));
//...additional code
output.Close();
}
實際上,您應該寫入本地用戶的“應用程序設置”或“文檔”文件夾,而不是根驅動器。 在Environment.SpecialFolder
使用ApplicationData
或Personal
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.