Ngoại lệ truy cập trái phép hệ thống

lập trình


Tôi đang sử dụng mã bên dưới để từ chối quyền truy cập. nhưng nó tạo ra "
System.UnauthorizedAccessException: Đã cố thực hiện thao tác trái phép.".

tôi đang chạy ứng dụng với tư cách là Quản trị viên. Vui lòng giúp tôi.

C#
string path=@"C:\Windows\System32\calc.exe;

AddDirectorySecurity(path, "everyone", FileSystemRights.ReadData, AccessControlType.Allow)


public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
   DirectoryInfo dInfo = new DirectoryInfo(FileName);
   DirectorySecurity dSecurity = dInfo.GetAccessControl();

   dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
   dInfo.SetAccessControl(dSecurity);
}

Ngoại lệ ở dòng này ” dInfo.SetAccessControl(dSecurity);”

Chi tiết ngoại lệ là:

System.UnauthorizedAccessException không được xử lý
Message=”Đã cố gắng thực hiện một thao tác trái phép.”
Nguồn=”mscorlib”
StackTrace:
tại System.Security.AccessControl.Win32.SetSecurityInfo(loại ResourceType, tên chuỗi, tay cầm SafeHandle, SecurityInfos securityInformation, chủ sở hữu SecurityIdentifier, nhóm SecurityIdentifier, GenericAcl sacl, GenericAcl dacl)
tại System.Security.AccessControl.NativeObjectSecurity.Persist(tên chuỗi, tay cầm SafeHandle, AccessControlSections includeSections, đối tượng ngoại lệContext)
tại System.Security.AccessControl.NativeObjectSecurity.Persist(tên chuỗi, AccessControlSections includeSections, đối tượng ngoại lệContext)
tại System.Security.AccessControl.NativeObjectSecurity.Persist(Tên chuỗi, AccessControlSections includeSections)
tại System.Security.AccessControl.FileSystemSecurity.Persist(Chuỗi fullPath)
tại System.IO.Directory.SetAccessControl(Chuỗi đường dẫn, DirectorySecurity thư mụcSecurity)
tại System.IO.DirectoryInfo.SetAccessControl(DirectorySecurity thư mụcSecurity)
tại usb_block_demo.Form1.AddDirectorySecurity(String FileName, String Account, FileSystemRights Rights, AccessControlType ControlType) trong C:\Users\Seema\Desktop\usb block demo\usb block demo\Form1.cs:line 135
tại usb_block_demo.Form1.button1_Click(Object sender, EventArgs e) trong C:\Users\Seema\Desktop\usb block demo\usb block demo\Form1.cs:line 104
tại System.Windows.Forms.Control.OnClick(EventArgs e)
tại System.Windows.Forms.Button.OnClick(EventArgs e)
tại System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
tại System.Windows.Forms.Control.WmMouseUp(Tin nhắn& m, nút MouseButtons, số lần nhấp chuột Int32)
tại System.Windows.Forms.Control.WndProc(Tin nhắn& m)
tại System.Windows.Forms.ButtonBase.WndProc(Tin nhắn& m)
tại System.Windows.Forms.Button.WndProc(Tin nhắn& m)
tại System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
tại System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
tại System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
tại System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
tại System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 lý do, Int32 pvLoopData)
tại System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (lý do Int32, bối cảnh ApplicationContext)
tại System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 lý do, bối cảnh ApplicationContext)
tại System.Windows.Forms.Application.Run(Form mainForm)
tại usb_block_demo.Program.Main() trong C:\Users\Seema\Desktop\usb block demo\usb block demo\Program.cs:line 17
tại System.AppDomain._nExecuteAssembly(Tập hợp, Chuỗi[] tranh luận)
tại System.AppDomain.ExecuteAssembly(Chuỗi tập tin, Bằng chứng hộiSecurity, Chuỗi[] tranh luận)
tại Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
tại System.Threading.ThreadHelper.ThreadStart_Context(Trạng thái đối tượng)
tại System.Threading.ExecutionContext.Run(ExecutionContext thực thiContext, gọi lại ContextCallback, trạng thái đối tượng)
tại System.Threading.ThreadHelper.ThreadStart()

Giải pháp 1

Theo tài liệu chính thức: http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessException.aspx[^]:

The reasons for the exception can be found in the details of the exception object.<br />
UnauthorizedAccessException uses the HRESULT COR_E_UNAUTHORIZEDACCESS, which has the value 0x80070005.

Tôi khuyên bạn nên xem chi tiết và/hoặc ngoại lệ bên trong mà bạn đang nhận được.
Tôi đoán rằng ngay cả với tư cách là quản trị viên cục bộ, bạn cũng có thể không có quyền cho phép bạn thực hiện hành động mà bạn đang cố gắng.

Chỉnh sửa: Vui lòng không đăng lại cùng một câu hỏi lần thứ hai. Hãy cập nhật câu hỏi ban đầu của bạn với nhiều thông tin hơn để chủ đề vẫn còn nguyên. (Câu hỏi gốc[^]

コメント

タイトルとURLをコピーしました