简体   繁体   English

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

[英]C# - Modify File Permission ASP.NET MVC

I want modify the file's permission, and I am talking about permission like 666 / 777 etc. In other words how tio change the permission of file from ANY to 666. The goal is changing the permission of uploaded file on my ASP.NET MVC Web App. 我想修改文件的权限,我正在谈论像666/777等权限。换句话说,如何将文件的权限从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;
    }
}

You should look at File.SetAccessControl Method 您应该查看File.SetAccessControl方法

public static void SetAccessControl(
    string path,
    FileSecurity fileSecurity
)

For example this is how you get the FileSecurity 's of a file 例如,这是您获取文件的FileSecurity的方式

FileSecurity fSecurity = File.GetAccessControl(filePath);

Then you create a FileSystemAccessRule 然后创建一个FileSystemAccessRule

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

And you add this Rule to the file's FileSecurity 然后将此规则添加到文件的FileSecurity

File.SetAccessControl(filePath, fSecurity);

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

Windows file security works differently to Unix, and there is no direct equivalent to chmod 666. Windows文件安全性与Unix的工作方式不同,并且没有直接等效于chmod 666。

The method you're looking for is File.SetAccessControl, which takes the path to the file, and a FileSecurity object. 您正在寻找的方法是File.SetAccessControl,它获取文件的路径和FileSecurity对象。 The closest equivalent to '666' is probably to assign read/write permissions to the 'Everyone' user account. 与“666”最接近的等价物可能是为“Everyone”用户帐户分配读/写权限。

To do this, you would use File.GetAccessControl to retrieve the current permissions for the file, add in the new read/write permissions to the FileSecurity object you just retrieved, and then use SetAccessControl to write the new permissions. 为此,您将使用File.GetAccessControl检索文件的当前权限,为刚刚检索的FileSecurity对象添加新的读/写权限,然后使用SetAccessControl写入新权限。

More information is available on MSDN here: http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol%28v=vs.110%29.aspx 有关MSDN的更多信息,请访问: http//msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM