string strRootFolder = "FolderPath";
AddDirectorySecurity(strRootFolder + "\\" + txtFolderName.Text, getAccountName(), getFolderRight(false, true), AccessControlType.Allow);
private FileSystemRights getFolderRight(bool isWrite, bool isRead)
{
try
{
if (isWrite)
return FileSystemRights.Write;
if (isRead)
return FileSystemRights.Read;
}
catch (Exception ex)
{
clsErrorHandling _clsErrorHandling = new clsErrorHandling();
_clsErrorHandling.AddExceptionDetail(ex, "DocumentRepository.aspx");
}
return FileSystemRights.Delete;
}
private string getAccountName()
{
string strAccountName = "";
try
{
strAccountName = @"Dataworld.co.in\Jain.r";
}
catch (Exception ex)
{
clsErrorHandling _clsErrorHandling = new clsErrorHandling();
_clsErrorHandling.AddExceptionDetail(ex, "DocumentRepository.aspx");
}
return strAccountName;
}
// Adds an ACL entry on the specified directory for the specified account.
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
try
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
catch (Exception ex)
{
clsErrorHandling _clsErrorHandling = new clsErrorHandling();
_clsErrorHandling.AddExceptionDetail(ex, "DocumentRepository.aspx");
}
}
// Removes an ACL entry on the specified directory for the specified account.
public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
try
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Remove the FileSystemAccessRule to the security settings.
dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
catch (Exception ex)
{
clsErrorHandling _clsErrorHandling = new clsErrorHandling();
_clsErrorHandling.AddExceptionDetail(ex, "DocumentRepository.aspx");
}
}
Comments
Post a Comment