2010-12-09 50 views
8

在我的網站我想創建一個新的文件夾,然後複製一些文件,並且我還想通過C#代碼創建一個html文件。我想知道如何檢查在C#文件夾的讀寫權限

  1. 如何檢查讀取和文件夾的寫權限
  2. 如何使用Asp.net MVC 2C#創建項目根

IM在運行時的HTML文件。

回答

15

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>"); 
1

爲什麼不使用App_Data,ASP.NET應用程序的身份自動具有讀寫權限?

+0

我如何檢查它對於.NET 3.5 SP1?我有錯誤: `'System.AppDomain'不包含'PermissionSet'的定義,並且沒有找到接受'System.AppDomain'類型的第一個參數的擴展方法'PermissionSet'(你是否缺少using指令或一個彙編參考?)`。 – 2012-11-25 15:26:06