Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
I am using this code for Unblocking Disk drive in system. it sometimes work and some time not. I block "D:" and then Unblock ,it easily unblock the drive. but when I blocked "E:" drive it blocks but do not unblock ,gives Exception "Unauthorised Access Exception.". I am using Window Xp
 

RemoveDirectorySecurity(@"E:\", "everyone", FileSystemRights.FullControl, AccessControlType.Deny);
public void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
       {
     DirectoryInfo dInfo = new DirectoryInfo(FileName);
 
               
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                dSecurity.SetAccessRuleProtection(false, false);
                dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                              Rights, InheritanceFlags.ContainerInherit |                          InheritanceFlags.ObjectInherit,
                                  PropagationFlags.None,
                              ControlType));
               
                dInfo.SetAccessControl(dSecurity);
}
Exception Details are:
 
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
 
************** Exception Text **************
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd)
at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)
at System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory)
at System.Security.AccessControl.DirectorySecurity..ctor(String name, AccessControlSections includeSections)
at System.IO.DirectoryInfo.GetAccessControl()
at PC_PROTECTION_PRO.Form4.RemoveDirectorySecurity(String FileName, String Account, FileSystemRights Rights, AccessControlType ControlType) in C:\Documents and Settings\Imbue\Desktop\pro demo\PC PROTECTION PRO\PC PROTECTION PRO\Form4.cs:line 1208
at PC_PROTECTION_PRO.Form4.RemoveAllSecurity() in C:\Documents and Settings\Imbue\Desktop\pro demo\PC PROTECTION PRO\PC PROTECTION PRO\Form4.cs:line 826
at PC_PROTECTION_PRO.Form4.button3_Click(Object sender, EventArgs e) in C:\Documents and Settings\Imbue\Desktop\pro demo\PC PROTECTION PRO\PC PROTECTION PRO\Form4.cs:line 927
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
 

************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
PC PROTECTION PRO
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Imbue/Desktop/pro%20demo/PC%20PROTECTION%20PRO/PC%20PROTECTION%20PRO/bin/Debug/PC%20PROTECTION%20PRO.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
Interop.Shell32
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Imbue/Desktop/pro%20demo/PC%20PROTECTION%20PRO/PC%20PROTECTION%20PRO/bin/Debug/Interop.Shell32.DLL
----------------------------------------
 
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
 
For example:
 



 
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Posted 15-Oct-12 1:36am
Edited 15-Oct-12 12:21pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Is it possible that on E: you are removing your own privileges as well? If that is the case then of course your application would not be able to re enable access.
  Permalink  
Comments
pradeep rasara at 16-Oct-12 3:11am
   
yes i also think same that i am loosing my privileges from E: drive. I loose the ownership from E: drive after running this code.
i am using below code to block E: drive.
can you please help me where i am doing wrong.
 
AddDirectorySecurity(@"E:\", "everyone", FileSystemRights.FullControl, AccessControlType.Deny);
 

public void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
 
try
{


DirectoryInfo dInfo = new DirectoryInfo(FileName);

 
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.SetAccessRuleProtection(true, false);
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
ControlType));
 

dInfo.SetAccessControl(dSecurity);
}



}
Marcus Kramer at 16-Oct-12 10:13am
   
You are going wrong by denying access to everyone. You are included in everyone, so you will lose access. You need to figure out how to perform this without using everyone as your role.

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100