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
  • 59,338
  • 27
  • 124
  • 151
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

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

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 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