C# - 修改文件权限 ASP.NET MVC

标签 c# asp.net-mvc permissions

我想修改文件的权限,我说的是 666/777 等权限。 换句话说,tio 如何将文件的权限从 ANY 更改为 666。 目标是在我的 ASP.NET MVC Web 应用程序上更改上传文件的权限。

  public string Uploadfile(HttpRequestBase currentRequest)
    {
        string fileName = "";
        for (int i = 0; i < currentRequest.Files.Count; i++)
        {
            if (currentRequest.Files[i].ContentLength > 0)
            {

                string strFileName = Guid.NewGuid().ToString() +
                                     Path.GetExtension(currentRequest.Files[i].FileName);
                currentRequest.Files[i].SaveAs(HttpContext.Current.Server.MapPath("/Upload/Task/" + strFileName));

                fileName = strFileName;
            }
        }
        return fileName;
    }
}

最佳答案

你应该看看 File.SetAccessControl 方法

public static void SetAccessControl(
    string path,
    FileSecurity fileSecurity
)

例如,这是您获取文件的 FileSecurity 的方式

FileSecurity fSecurity = File.GetAccessControl(filePath);

然后你创建一个FileSystemAccessRule

FileSystemAccessRule rule = new FileSystemAccessRule(SPECIFIC_USER, FileSystemRights.FullControl, AccessControlType.Allow);

然后您将此规则添加到文件的 FileSecurity

File.SetAccessControl(filePath, fSecurity);

FileSystemRights 可能值列表
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx

关于C# - 修改文件权限 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27505335/

相关文章:

c# - Linq To Twitter 清除 IOAuthCredentials 凭据

c# - 您是否在任何地方都使用 1-3 个字母的变量?

c# - 无法将文件上传到 Google 云端硬盘

asp.net-mvc - 多页/表单MVC Web应用程序的最佳实践

azure - 更改 Azure 应用服务中的 AppPool 文件夹权限 - IIS APPPOOL\MyUserName

c# - 为移动 Sprite 计时

c# - 创建后无法连接到 Microsoft SQL Server 数据库 ("The system cannot find the file specified")

javascript - 为什么我的 MVC Controller 上的数组在 Ajax 回传上为空

unix - 如何解决/var/www复制/写入权限被拒绝的问题?

apache - Perl 不允许写入文件