3

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.

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

2 Answers 2

1

You should look at File.SetAccessControl Method

public static void SetAccessControl(
    string path,
    FileSecurity fileSecurity
)

For example this is how you get the FileSecurity's of a file

FileSecurity fSecurity = File.GetAccessControl(filePath);

Then you create a FileSystemAccessRule

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

And you add this Rule to the file's 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

Sign up to request clarification or add additional context in comments.

2 Comments

This Code allows Read & Write, but does it disallow the Execute?
FullControl includes execute. I have updated my answer to include a link to possible values for FileSystemRights.
0

Windows file security works differently to Unix, and there is no direct equivalent to chmod 666.

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

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.