. Net (C #): Use the ram API to delete the trustedinstaller permission folder that remains in another Windows system.

Source: Internet
Author: User
Tags trustedinstaller

Note:

There are multiple ways to delete files (or folders) that are not authorized by the current user. Here we only discuss how to use the access control API in. Net to do the above. Other methods are not evaluated.

 

The reason is that Windows 8 is installed and you want to uninstall it. Because Windows 8 is installed on drive F, and drive F contains other important files, it cannot be formatted, after you delete Windows 8 from the startup Item, You need to manually delete the folders left over from Windows 8.

 

The problem is that some (majority) folders and files are all owned by a "trustedinstaller". The current user has only limited permissions on them. In fact, the user group of the current computer (users user group) only has read-only permission on the folder content, while the current user group administrator user group (administrators user group) has the right to set the owner.

 

First, you must change the folder owner to your own user account (instead of the trustedinstaller account ).

Note:

I tried to use.. Net Access Control API to set the owner (when the target owner is not the current user, such as trustedinstaller in this example), even if the program runs as an administrator, the system still throws a permission exception (system. unauthorizedaccessexception), so it seems that the modification can only be performed in the Windows Properties dialog box. (Of course, I hope I am wrong. If you know how to change the trustedinstaller account to the Current Account under. net, please leave a message to tell me)

 

In this case, the security option card in the Windows folder (or file) attribute is used to change the owner to its own account.

 

On the Security tab, select Advanced:

 

Then, this dialog box appears, and select the owner (I don't know if the owner in the Chinese system is the "owner", but it doesn't matter. It means similar, you certainly won't find it ):

 

The current user account is listed above. Click the edit below to change the owner to his/her account. (Finally, apply the owner to the sub-files and folders.) For example:

 

 

Next, wait for Windows to set the owner to the current user:

 

Note that some files cannot be deleted at this time, because the permissions of the Current Account are still insufficient (only read-only permissions), such:

 

You can use the security option card in the properties to view the definition of access control permissions for folders. Currently, users only have read-only and modify owner permissions.

 

The next step is to remove all these restrictions in the folder. we will delete the custom access control permission definitions for the operation. This allows you to use the knowledge (. net (C #): sets File System Object Access Control). We use the method at the end of the text to clear custom access control options, as follows:

// + Using system. IO;

// + Using system. Security. Principal;

// + Using system. Security. accesscontrol;

Static void clearmyaccessrules (string folder)

{

Foreach (var file in directory. getfiles (folder ))

{

// Create an empty ACL

VaR filesecurity = new filesecurity ();

// Set inheritance

Filesecurity. setaccessruleprotection (false, true );

// Set the file

File. setaccesscontrol (file, filesecurity );

}

Foreach (VAR dir in directory. getdirectories (folder ))

{

// Create an empty ACL

VaR dirsecurity = new directorysecurity ();

// Set inheritance

Dirsecurity. setaccessruleprotection (false, true );

// Set the folder

Directory. setaccesscontrol (Dir, dirsecurity );

 

Clearmyaccessrules (DIR );

}

}

 

Use the above method to clear the definition of access control for all subfolders and subfolders in the folder:

Clearmyaccessrules ("F: \ Program Files ");

 

 

After it is executed (it may take some time, if the folder is large), let's look at the attributes of the stubborn files:

Now we have all permissions.

 

Finally, delete the folder directly. After all the operations are completed successfully, no errors such as "no permission" will be prompted.

It is interesting to use the learned programming knowledge to write programs to solve computer problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.