7

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#.

Øyvind Bråthen
  • 56,684
  • 27
  • 122
  • 146
Fraz Sundal
  • 9,898
  • 22
  • 80
  • 131

2 Answers2

17

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>");
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
1

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

Massimiliano Peluso
  • 25,679
  • 6
  • 57
  • 68
  • How can I check it for .Net 3.5 SP1? I have got error: `'System.AppDomain' does not contain a definition for 'PermissionSet' and no extension method 'PermissionSet' accepting a first argument of type 'System.AppDomain' could be found (are you missing a using directive or an assembly reference?)`. – Andrey Bushman Nov 25 '12 at 15:26
  • app_data doesn't have write permission by default for hosted apps – HasanG Apr 27 '17 at 18:45