4

在此处输入图像描述

我有服务器-客户端应用程序,它是一个文件管理
器获得这样做的许可?

这是我在服务器端创建新文件夹的方法

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");
        }
    }
4

7 回答 7

4

驱动器上经常有一些目录,即使是具有管理员权限的用户也无法访问。像“HDDRecovery”这样的目录很可能会像这样麻烦。当然,它包含有助于用户从磁盘故障中恢复的敏感数据。另一个适合此类别的目录是“c:\system volume information”,它包含还原点数据。

管理员可以像这样更改文件夹的权限。但这当然不能解决真正的问题,也不是明智的做法。您的用户不能也不应该。确保编写处理此类权限问题的代码,只需捕获 IOExeption。永远不要显示设置了 Hidden 或 System 属性的目录,从而避免用户遇到麻烦。它们是“别惹我”的属性。

于 2012-02-25T14:42:21.963 回答
4

如果要删除目录只读属性,请使用:http ://social.msdn.microsoft.com/Forums/en/vblanguage/thread/cb75ea00-f9c1-41e5-ac8e-296c302827a4

如果您想访问系统文件夹,您可以以本地管理员身份运行您的程序。

于 2012-02-25T13:19:14.620 回答
4

这段代码我遇到了类似的问题(asp.net MVC vs2017):

Directory.CreateDirectory("~/temp");

这是我的解决方案:

// Create path on your web server
System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/temp"));
于 2018-07-24T12:03:02.927 回答
3

我也遇到了与此类似的问题,但我能够通过 Windows 资源管理器手动导航并创建目录。

但是,我的 Web 应用程序在笔记本电脑上的 VS 中运行,通过我的本地 IIS 而不是 VS 的内置 IIS 交易托管,触发了访问被拒绝问题。

因此,当我遇到代码错误时,我深入研究以从 System.Environment 对象中收集更多数据并找到用户,这当然是我的应用程序在 IIS 中运行的应用程序池。

所以我打开 IIS 并打开有问题的应用程序池的高级设置,并将身份更改为在网络服务下运行。单击确定。“cmd -> iisreset”是很好的衡量标准。再次尝试应用程序,成功!!!!

于 2014-02-03T19:02:17.963 回答
1

创建目录时我遇到了同样的问题。我使用了 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");
    }
}
于 2015-02-09T00:06:54.243 回答
0

我的怀疑是,当您在客户端/服务器模式下运行应用程序时,除了可能删除只读或系统标志之外,服务器部分还需要以管理员身份运行,才能执行您想要的操作。

也就是说,我同意@HansPassant - 听起来你正在尝试做的事情是不明智的。

于 2012-02-25T15:54:21.490 回答
0

已解决: 使用以下代码和设置在远程服务器上创建的目录。

共享文件夹并在文件夹的高级设置中授予完全权限。

DirectoryInfo di = Directory.CreateDirectory(@"\\191.168.01.01\Test\Test1");

Test 是创建新 Test1 文件夹(目录)的目标文件夹

于 2017-03-15T06:37:19.633 回答