PDA

View Full Version : سوال: تغییر permision فایل ها و پوشه ها به صورت داینامیک



moferferi
یک شنبه 01 آبان 1390, 18:03 عصر
سلام.
توی iis و کنترل پنل هاست میشه پرمیشن فایل ها و پوشه ها را تغییر داد.
روشی هست که بشه از طریق برنامه نویسی این کار را کرد.
من میخوام فقط در هنگام تغییر مقادیر فایل های xml یا فایل وب کانفیگ مجوز رایت داشته باشن.
و بعد از ذخیره دوباره اون ها را به حالت readonly تغییر بدم.
البته سرچ کردم و این کد را پیدا کردم .نام یه فایل را میگیره ولی نمیدونم باهاش چی کار میکنه(:خجالت:)
using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string fileName = "test.xml";

Console.WriteLine("Adding access control entry for "
+ fileName);

// Add the access control entry to the file.
AddFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Removing access control entry from "
+ fileName);

// Remove the access control entry from the file.
RemoveFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.ReadData, AccessControlType.Allow);

Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{


// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);

// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));

// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);

}

// Removes an ACL entry on the specified file for the specified account.
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{

// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);

// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));

// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);

}
}
}


منبع کد (http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx)