C # - change permissions on ASP.NET MVC files

I want to change the resolution of a file and I'm talking about a resolution like 666/777 etc. In other words, how tio changes the resolution of the file from ANY to 666. The goal is to change the resolution of the uploaded file in my ASP.NET MVC web application.

  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;
    }
}

      

+3


source to share


2 answers


You should see File.SetAccessControl

Method

public static void SetAccessControl(
    string path,
    FileSecurity fileSecurity
)

      

For example, you get the FileSecurity

file

FileSecurity fSecurity = File.GetAccessControl(filePath);

      

Then you create FileSystemAccessRule



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

      

And you add this rule to the file FileSecurity

File.SetAccessControl(filePath, fSecurity);

      

List of FileSystemRights

possible values
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx

+1


source


Windows file protection works differently with Unix and there is no direct equivalent to chmod 666.

The method you're looking for is File.SetAccessControl, which takes a file path and a FileSecurity object. The closest equivalent to "666" should probably assign read / write permissions to the "Everyone" user account.



To do this, you must use File.GetAccessControl to get the current file permissions, add new read / write permissions to the FileSecurity object you just got, and then use SetAccessControl to write the new permissions.

More information is available on MSDN here: http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol%28v=vs.110%29.aspx

0


source







All Articles