Set user access (C #) for Windows system NTFS a directory

xiaoxiao2021-03-06  58

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.management;

Using system.text;

Using system.Runtime.InteropServices;

Namespace setaCl

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.Button setAn;

Private system.windows.Forms.Label label1;

Private system.windows.Forms.Label label2;

Private system.windows.Forms.Button button1;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.TextBox1 = new system.windows.Forms.TextBox ();

This.TextBox2 = new system.windows.Forms.TextBox ();

This.SetaCl = new system.windows.Forms.Button ();

THIS.LABEL1 = New System.windows.Forms.label ();

THIS.LABEL2 = New System.windows.Forms.label ();

This.button1 = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// textbox1

//

This.TextBox1.Location = new system.drawing.point (80, 32);

THIS.TEXTBOX1.NAME = "textBox1"; this.textBox1.size = new system.drawing.size (152, 21);

this.TextBox1.tabindex = 0;

THIS.TEXTBOX1.TEXT = "C: // Test";

//

// textbox2

//

This.TextBox2.Location = new system.drawing.point (80, 80);

THIS.TEXTBOX2.NAME = "TextBox2";

This.TextBox2.size = new system.drawing.size (152, 21);

this.TextBox2.tabindex = 1;

this.TextBox2.text = "aspnet";

//

// setaCl

//

This.SetaCl.Location = new system.drawing.point (152, 136);

This.SetaCl.name = "setacl";

THIS.SetaCl.tabindex = 2;

This.SetaCl.Text = "Set";

This.SetaCl.Click = new system.eventhandler (this.setAcl_click);

//

// label1

//

THIS.Label1.Location = new system.drawing.point (24, 80);

THIS.LABEL1.NAME = "label1";

THIS.Label1.size = new system.drawing.size (48, 23);

THIS.Label1.tabindex = 5;

THIS.Label1.Text = "User:";

//

// label2

//

THIS.Label2.Location = new system.drawing.point (24, 32);

THIS.LABEL2.NAME = "label2";

THIS.Label2.size = new system.drawing.size (48, 23);

THIS.Label2.tabindex = 6;

This.Label2.Text = "directory:";

//

// Button1

//

This.Button1.Location = new system.drawing.point (8, 168);

This.button1.name = "button1";

this.button1.tabindex = 7;

This.Button1.Text = "Button1";

This.Button1.click = new system.eventhandler (this.button1_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

this.clientsize = new system.drawing.size (280, 197);

This.Controls.add (this.button1);

THIS.CONTROLS.ADD (this.label2);

This.Controls.add (this.label1);

This.Controls.add (this.setaCl);

This.Controls.add (this.TextBox2);

This.Controls.add (this.TextBox1);

THIS.MAXIMIZEBOX = FALSE;

THIS.MINIMIZEBOX = FALSE;

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

[DLLIMPORT ("Advapi32.dll")]]]]]

public static extern bool LookupAccountName (string lpSystemName, string lpAccountName, byte [] sid, ref int cbSid, StringBuilder ReferencedDomainName, ref int cbReferencedDomainName, ref int peUse);

Private void setaCl_click (Object Sender, System.Eventargs E)

{

String filepath = this.TextBox1.text;

String username = this.TextBox2.text;

IF (getFileSystem (filepath.substring (0, 1))! = "NTFS")

Return;

IF (FilePath, Username)

Return;

SetDacl (filepath, username);

}

Private string getFileSystem (String DiskName)

{

String filesystem = "";

System.Management.ManagementObjectSearcher DiskClass = New ManagementObjectSearcher ("SELECT FILESYSTEM WIN32_LOGICALDISTEM WHERE Name = '" DISKNAME ":");

ManagementObjectCollection Disks = diskclass.get ();

Forum (ManagementObject Disk in Disks)

{

PropertyDataCollection DiskProperties = Disk.properties;

Foreach (PropertyData DiskProperty in DiskProperties)

{

FileSystem = DiskProperty.Value.toString ();

}

}

Return FileSystem;

}

Private Bool FindTrustee (String Filepath, String Username)

{

Arraylist trusteesname = new arraylist ();

ManagementPath path = new managementPath ();

Path.server = "."

Path.namespacepath = @ "root / cimv2";

Path.RELATIVEPATH = @ "Win32_LogicalFileSecuritySetting.path = '" filepath "'"; // Position to folder

ManagementObject Dir = New ManagementObject (PATH);

ManagementBaseObject Outparams = Dir.InvokeMethod ("GetSecurityDescriptor", null, null; // Get security descriptor

IF ((UINT) ("ReturnValue") ("ReturnValue")! = 0 // OK

{

Throw New Exception ("Get File Descriptor Failure");

}

ManagementBaseObject descriptor = (("" "" "" "" ""); "Descriptor"]. Value);

ManagementBaseObject [] daclobject = ((ManagementBaseObject []) (Descriptor.properties ["DACL"]. Value); // Get access control list

For (int i = 0; i

{

TrusteesName.Add ((managementBaseObject) Daclobject [i] .properties ["trustee"]. value) .properties ["name"]. value);

}

Return trusteesname.contains (username);

}

Private void setdacl (string filepath, string username)

{

// Get account information

INT CBSID = 100;

Byte [] usersid = new byte [28];

StringBuilder DomainName = New StringBuilder (255);

INT DomainNameLength = 255;

Int SidType = 255;

Bool Result = lookupaccountname (Null, Username, Usersid, Ref CBSID, DomainName, Ref DomainNameLength, ref Sidtype);

IF (! result)

Return;

// Get a file descriptor

ManagementPath path = new managementPath ();

Path.server = "."

Path.namespacepath = @ "root / cimv2";

Path.RELATIVEPATH = @ "Win32_LogicalFileSecuritySetting.path = '" FilePath "'"

ManagementObject Dir = New ManagementObject (PATH);

ManagementBaseObject Outparams = Dir.InvokeMethod ("GetSecurityDescriptor", NULL, NULL); if (((UINT) (outparams.properties ["ReturnValue"]. Value))! = 0)

{

Throw New Exception ("Get File Descriptor Failure");

}

ManagementBaseObject descriptor = (("" "" "" "" ""); "Descriptor"]. Value);

// Get access control list

ManagementBaseObject [] daclobject = ((ManagementBaseObject []) (Descriptor.properties ["DACL"]. Value);

/ / Copy an access control item

ManagementBaseObject ace = (managementBaseObject) Daclobject [0] .clone ();

/ / Set access control item properties

ManagementBaseObject trustee = (managementBaseObject) ace.properties ["trustee"]. Value;

Trustee.properties ["domain"]. Value = domainname.toString ();

Trustee.properties ["name"]. Value = username

Trustee.properties ["SID"]. Value = UserSid;

Trustee.properties ["SIDLENGTH"]. Value = 28; // trustee.properties ["Sidstring"]. Value = "S-1-5-21-602162358-70889826-854245398-1005";

Ace.properties ["trustee"]. Value = trustee;

Ace.properties ["Accessmask"]. Value = 2032127;

Ace.properties ["aceflags"]. Value = 3;

Ace.properties ["ACETYPE"]. Value = 0;

/ / Copy a list of access controls and add the above-generated access control items to it.

ManagementBaseObject [] newdacl = new managementBaseObject [daclobject.length 1];

For (int i = 0; i

{

NewDaCl [I] = DACLOBJECT [I];

}

NewDacl [Daclobject.length] = ACE;

// Set the DACL property of the security descriptor to the newly generated access control list

Descriptor.properties ["DACL"]. Value = NewDacl;

/ / Set security descriptor

Dir.scope.Options.enableprivileges = true;

ManagementBaseObject Inproperties = Dir.getMethodParameters ("SetSecurityDescriptor");

Inproperties ["Descriptor"] = Descriptor; Outparams = Dir.InvokeMethod ("SetSecurityDescriptor", Inproperties, NULL)

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

System.Management.ManagementObjectSearcher CMICWMI = New System.Management.ManagementObjectSearcher ("Select * from win32_baseboard");

FOREACH (System.Management.ManagementObject Cmanagement.ManagementObject Cmicwmiobj In Cmicwmi.get ())

{

//Debug.writeline ("bios serial number" & cmanWmiobj ["serialnumber"]);

THIS.TEXTBOX1.TEXT = "BIOS Serial Number" CMICWMIOBJ ["Product"];

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-109184.html

New Post(0)