Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Read and write permissions on folder in C#

Tags:

c#

asp.net-mvc

In my website i want to create a new folder and then copy some files in it and also i want to create an html file through C# code. I want to know

  1. How to check read and write permission on a folder
  2. How to create an html file on runtime in project root

Im using Asp.net MVC 2 and C#.

like image 907
Fraz Sundal Avatar asked Dec 09 '10 09:12

Fraz Sundal


People also ask

How do I check folder permissions?

Right-click on any file/folder, and select “Properties”. To check the permissions, head to the “Permission” tab.

What does chmod 644 mean?

Permissions of 644 mean that the owner of the file has read and write access, while the group members and other users on the system only have read access.

How do I check permissions 755?

755 means read and execute access for everyone and also write access for the owner of the file. When you perform chmod 755 filename command you allow everyone to read and execute the file, the owner is allowed to write to the file as well.

What is the chmod 777 means?

Some file permission examples: 777 - all can read/write/execute (full access). 755 - owner can read/write/execute, group/others can read/execute. 644 - owner can read/write, group/others can read only.


2 Answers

How to check read and write permission on a folder

string folder = ...

var permission = new FileIOPermission(FileIOPermissionAccess.Write, folder);
var permissionSet = new PermissionSet(PermissionState.None);
permissionSet.AddPermission(permission);
if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
{
    // You have write permission for the given folder
}

How to create an html file on runtime in project root

string file = Path.Combine(Server.MapPath("~/"), "foo.html");
File.WriteAllText(file, "<html><body><h1>Hello</h1></body></html>");
like image 136
Darin Dimitrov Avatar answered Sep 24 '22 01:09

Darin Dimitrov


why don't you use App_Data where the identity of ASP.NET application automatically has read and write permissions?

like image 40
Massimiliano Peluso Avatar answered Sep 22 '22 01:09

Massimiliano Peluso