C# Directory.CreateDirectory 对路径的访问被拒绝?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9444270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Directory.CreateDirectory access to path is denied?
提问by Murhaf Sousli


I have server-client application, it's a file manager
my problem is when I go inside a folder which requires access control like system folders, it becomes to read-only, but I need to move/delete or create new folder, how can I get the permission to do that?
我有服务器-客户端应用程序,它是一个文件管理器,
我的问题是当我进入需要访问控制的文件夹(如系统文件夹)时,它变为只读,但我需要移动/删除或创建新文件夹,我该怎么办获得许可?
here's how I create a new folder at the server side
这是我在服务器端创建新文件夹的方法
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");
}
}
采纳答案by Hans Passant
There are often directories on a drive that even a user with administrator privileges cannot access. A directory with a name like "HDDRecovery" is quite likely to be troublesome like this. Surely it contains sensitive data that helps the user recover from disk failure. Another directory that fits this category is "c:\system volume information", it contains restore point data.
驱动器上通常存在即使具有管理员权限的用户也无法访问的目录。像“HDDRecovery”这样的目录很可能会像这样麻烦。当然,它包含帮助用户从磁盘故障中恢复的敏感数据。另一个适合此类别的目录是“c:\system volume information”,它包含还原点数据。
An admin can change the permissions on folders like this. But of course that doesn't solve the real problem nor is it a wise thing to do. Your user can't and shouldn't. Be sure to write code that deals with permission problems like this, simply catch the IOExeption. Keep the user out of trouble by never showing a directory that has the Hidden or System attribute set. They are the "don't mess with me" attributes.
管理员可以像这样更改文件夹的权限。但这当然不能解决真正的问题,也不是明智之举。您的用户不能也不应该。请务必编写处理此类权限问题的代码,只需捕获 IOExeption。通过从不显示设置了 Hidden 或 System 属性的目录,让用户远离麻烦。它们是“不要惹我”的属性。
回答by user1227804
If you want to remove directory read-only attribute use this: http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/cb75ea00-f9c1-41e5-ac8e-296c302827a4
如果要删除目录只读属性,请使用:http: //social.msdn.microsoft.com/Forums/en/vblanguage/thread/cb75ea00-f9c1-41e5-ac8e-296c302827a4
If you want to access system folders you can run your program as local administrator.
如果您想访问系统文件夹,您可以以本地管理员身份运行您的程序。
回答by Chris Shain
My suspicion is that when you are running the application in client/server mode, the serverportion needs to be running as Administrator, in addition to possibly removing read-only or system flags, to be able to do what you want.
我怀疑当您在客户端/服务器模式下运行应用程序时,除了可能删除只读或系统标志之外,服务器部分还需要以管理员身份运行,以便能够执行您想要的操作。
That said, I agree with @HansPassant- it sounds like what you are trying to do is ill-advised.
也就是说,我同意@HansPassant-听起来您尝试做的事情是不明智的。
回答by Joe de la Forms
I also ran into an issue similar to this, but I was able to manually navigate through Windows Explorer and create directories.
我也遇到了与此类似的问题,但我能够手动浏览 Windows 资源管理器并创建目录。
However, my web app, running in VS on my laptop, hosted through my local IIS and not the built-in IIS deal for VS, was triggering the Access Denied issue.
但是,我的 Web 应用程序在我的笔记本电脑上的 VS 中运行,通过我的本地 IIS 托管,而不是 VS 的内置 IIS 交易,触发了拒绝访问问题。
So when I was hitting the error in code, I drilled down to glean more data from the System.Environment object and found the user, which of course was the App Pool that my app was running under in IIS.
因此,当我遇到代码中的错误时,我深入研究以从 System.Environment 对象中收集更多数据并找到用户,这当然是我的应用程序在 IIS 中运行的应用程序池。
So I opened IIS and opened the Advanced Settings for the app pool in question and changed the Identity to run under Network Service. Click OK. "cmd -> iisreset" for good measure. Try the app again, and SUCCESS!!!!
因此,我打开了 IIS 并打开了相关应用程序池的高级设置,并将身份更改为在网络服务下运行。单击确定。“cmd -> iisreset”是很好的衡量标准。再次尝试该应用程序,并成功!!!!
回答by Dush
I had the same issue when creating a directory. I used DirectorySecurity as shown below:
创建目录时我遇到了同样的问题。我使用 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);
Also keep in mind about the security as explained by Hans Passant's answer.
还要记住Hans Passant 的回答所解释的安全性。
Full details can be found on MSDN.
可以在MSDN上找到完整的详细信息。
So the complete code:
所以完整的代码:
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");
}
}
回答by Abdul Khaliq
Solved:Directory created on remote server using below code & setting.
已解决:使用以下代码和设置在远程服务器上创建的目录。
Share folder and give the full permission rights also in Advance setting in the folder.
共享文件夹并在文件夹中的高级设置中授予完全权限。
DirectoryInfo di = Directory.CreateDirectory(@"\191.168.01.01\Test\Test1");
Test is destination folder where to create new Test1 folder(directory)
Test 是创建新 Test1 文件夹(目录)的目标文件夹
回答by Darren
I had a similar problem (asp.net MVC vs2017) with this code:
我在这段代码中遇到了类似的问题(asp.net MVC vs2017):
Directory.CreateDirectory("~/temp");
Here is my solution:
这是我的解决方案:
// Create path on your web server
System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/temp"));

