Click here to Skip to main content
14,935,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a drive called Z: which is present in a lan and it should be accessible to all systems connected in the LAN.

When I create a folder in Z:, I want to set the permissions (ie) read, hidden, etc...

I am creating the directory using System.IO.Directory.CreateDirectory() method.

I tried the following methods to set the permissions, but hasn't work for the network drive.

public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
    try
    {
       //http://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol.aspx
       // Create a new DirectoryInfoobject.
       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 e)
    {
       MessageBox.Show(e.Message.ToString());
    }
}

public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
    // 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.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

    // Set the new access settings.
    dInfo.SetAccessControl(dSecurity);

}



How do I set the permissions using c# 3.0? Any help would be appreciated.

Thanks in advance,
Pawan.
Posted
Updated 27-Apr-10 5:30am
v2

Is your network folder hosted on an NTFS share? This won't work if it's some other share (like consumer NAS devices), or even most versions of Samba.
Even if your share is NTFS, you cannot set permissions on the root of the share (these are controlled by the share itself, and the permission are quite simple: read, read/write or no access.)
You can only set permissions on subdirectories and files below the root of the share.
   
v2
:laugh: This is how I do it...

<br />
		public static bool SetFolderAccess(<br />
			string fullPath<br />
			, string userDomainName<br />
			, System.Security.AccessControl.FileSystemRights systemRights<br />
			, System.Security.AccessControl.InheritanceFlags inheritanceFlags<br />
			, System.Security.AccessControl.PropagationFlags propagationFlags<br />
			, System.Security.AccessControl.AccessControlType accessControlType<br />
			, out string statusMessage)<br />
		{<br />
			/*==============================================================================\<br />
			 * Description: called to set the access control to a folder ... see sample<br />
			 * Comment: User of app must have admin or folder owner set modify permissions<br />
			 * --------------------------------  Notes  ------------------------------------ <br />
			 * -----------------------------------------------------------------------------<br />
			 * Sample call:<br />
			 *		Security.AccessControl.SetFolderAccess(<br />
						appsFullPath //--- "C:\logs\C"<br />
						, userDomainName //--- ""<br />
						, System.Security.AccessControl.FileSystemRights.Modify<br />
						, System.Security.AccessControl.InheritanceFlags.ContainerInherit<br />
							| System.Security.AccessControl.InheritanceFlags.ObjectInherit<br />
						, System.Security.AccessControl.PropagationFlags.None<br />
						, System.Security.AccessControl.AccessControlType.Allow<br />
						, out statusMessage);<br />
			\==============================================================================*/<br />
<br />
			//--- declarations<br />
			bool returnSuccess = true;<br />
			System.Security.AccessControl.FileSystemAccessRule accessRule = null;<br />
			System.IO.DirectoryInfo directoryInfo = null;<br />
			System.Security.AccessControl.DirectorySecurity directorySecurity = null;<br />
<br />
			//--- initialize<br />
			statusMessage = "SUCCESS";<br />
			fullPath = MTC.Utils.RemoveSlashFromPath(fullPath);<br />
<br />
			try<br />
			{<br />
				if (System.IO.Directory.Exists(fullPath) == false)<br />
				{<br />
					statusMessage += "FAILED: Path cannot be empty.";<br />
					returnSuccess = false;<br />
					throw new Exception(statusMessage);<br />
				}<br />
<br />
				//--- Add Access Rule to the actual directory itself<br />
				accessRule =<br />
					new System.Security.AccessControl.FileSystemAccessRule(<br />
						userDomainName<br />
						, systemRights<br />
						, inheritanceFlags<br />
						, propagationFlags<br />
						, accessControlType);<br />
<br />
				directoryInfo =<br />
					new System.IO.DirectoryInfo(<br />
						fullPath);<br />
<br />
				directorySecurity =<br />
					directoryInfo.GetAccessControl(<br />
						System.Security.AccessControl.AccessControlSections.Access);<br />
<br />
				directorySecurity.ModifyAccessRule(<br />
					System.Security.AccessControl.AccessControlModification.Set<br />
					, accessRule<br />
					, out returnSuccess);<br />
<br />
				if (!returnSuccess)<br />
				{<br />
					statusMessage += "FAILED: Unable to modify the folder security";<br />
					throw new Exception(statusMessage);<br />
				}<br />
<br />
				directoryInfo.SetAccessControl(directorySecurity);<br />
			}<br />
<br />
			catch(Exception eo)<br />
			{<br />
				if (statusMessage.Length.Equals(0))<br />
				{<br />
					statusMessage = "FAILED: Unexpected error..Unable to modify the folder security";<br />
				}<br />
				returnSuccess = false;<br />
			}<br />
<br />
			return returnSuccess;<br />
		}<br />
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900