![](/img/trans.png)
[英]Access denied on Directory.CreateDirectory from ASP.Net application
[英]Directory.CreateDirectory access to path is denied?
我有服務器 - 客戶端應用程序,它是一個文件管理器
我的問題是,當我進入一個需要訪問控制的文件夾,如系統文件夾,它變為只讀,但我需要移動/刪除或創建新文件夾,我怎么能獲得這樣做的權限?
這是我在服務器端創建一個新文件夾的方法
public void NewFolder(string path)
{
try
{
string name = @"\New Folder";
string current = name;
int i = 0;
while (Directory.Exists(path + current))
{
i++;
current = String.Format("{0} {1}", name, i);
}
Directory.CreateDirectory(path + current);
Explore(path); //this line is to refresh the items in the client side after creating the new folder
}
catch (Exception e)
{
sendInfo(e.Message, "error");
}
}
如果要刪除目錄只讀屬性,請使用以下命令: http : //social.msdn.microsoft.com/Forums/en/vblanguage/thread/cb75ea00-f9c1-41e5-ac8e-296c302827a4
如果要訪問系統文件夾,可以以本地管理員身份運行程序。
驅動器上經常有目錄,即使具有管理員權限的用戶也無法訪問。 名為“HDDRecovery”的目錄很可能像這樣麻煩。 當然,它包含敏感數據,可幫助用戶從磁盤故障中恢復。 適合此類別的另一個目錄是“c:\\ system volume information”,它包含還原點數據。
管理員可以更改此類文件夾的權限。 但當然,這並不能解決實際問題,也不是明智之舉。 您的用戶不能也不應該。 一定要編寫處理此類權限問題的代碼,只需捕獲IOExeption即可。 從不顯示設置了Hidden或System屬性的目錄,從而使用戶免於麻煩。 他們是“不要惹我”的屬性。
我也遇到了類似的問題,但我能夠手動瀏覽Windows資源管理器並創建目錄。
但是,我的筆記本電腦上的VS中運行的Web應用程序通過我的本地IIS托管,而不是VS的內置IIS交易,觸發了Access Denied問題。
因此,當我在代碼中遇到錯誤時,我深入研究了System.Environment對象中的更多數據並找到了用戶,當然這是我的應用程序在IIS中運行的應用程序池。
因此,我打開了IIS並打開了相關應用程序池的高級設置,並將標識更改為在網絡服務下運行。 單擊確定。 “cmd - > iisreset”是一個很好的衡量標准。 再試一次應用程序,成功!!!!
創建目錄時遇到了同樣的問題。 我使用DirectorySecurity,如下所示:
DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\AdminAccount1", FileSystemRights.Read, AccessControlType.Allow));
securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\YourAppAllowedGroup", FileSystemRights.FullControl, AccessControlType.Allow));
DirectoryInfo di = Directory.CreateDirectory(path + current, securityRules);
還要記住Hans Passant的答案所解釋的安全問題 。
完整的詳細信息可以在MSDN上找到。
那么完整的代碼:
public void NewFolder(string path)
{
try
{
string name = @"\New Folder";
string current = name;
int i = 0;
while (Directory.Exists(path + current))
{
i++;
current = String.Format("{0} {1}", name, i);
}
//Directory.CreateDirectory(path + current);
DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\AdminAccount1", FileSystemRights.Read, AccessControlType.Allow));
securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\YourAppAllowedGroup", FileSystemRights.FullControl, AccessControlType.Allow));
DirectoryInfo di = Directory.CreateDirectory(path + current, securityRules);
Explore(path); //this line is to refresh the items in the client side after creating the new folder
}
catch (Exception e)
{
sendInfo(e.Message, "error");
}
}
我有一個類似的問題(asp.net MVC vs2017)與此代碼:
Directory.CreateDirectory("~/temp");
這是我的解決方案:
// Create path on your web server
System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/temp"));
已解決:使用以下代碼和設置在遠程服務器上創建的目錄。
共享文件夾並在文件夾中的高級設置中授予完全權限。
DirectoryInfo di = Directory.CreateDirectory(@"\\191.168.01.01\Test\Test1");
Test是目標文件夾,在哪里創建新的Test1文件夾(目錄)
我懷疑當您在客戶端/服務器模式下運行應用程序時, 服務器部分需要以管理員身份運行,除了可能刪除只讀或系統標志之外,還能夠執行您想要的操作。
也就是說,我同意@HansPassant-聽起來你想做的事情是不明智的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.