[Work records] c # operate the win7 Registry

Source: Internet
Author: User

Here I will talk aboutC #Small column child (Attackers can bypass this area!), We usually develop a lot in xp, but now many developers have developed it in win7.

Below isLocalMachineThe following registry operations will not be detailed.

Private static void DeleteRegistry (string name)
{
String [] aimnames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("Microsoftss", true );
Aimnames = aimdir. GetSubKeyNames ();
Foreach (string aimKey in aimnames)
{
If (aimKey = name) during-pregnancy
Aimdir. DeleteSubKeyTree (name );
}
}

Private static string GetData (string name)
{
String registData;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("Microsoftss", true );
RegistData = aimdir. GetValue (name). ToString ();
Return registData;
}

Private static void WriteValue (string name, string tovalue)
{
RegistryKey hklm = Registry. LocalMachine;
RegistryKey software = hklm. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. CreateSubKey ("Microsoftss ");
Aimdir. SetValue (name, tovalue );
}

Private static bool IsExit (string name)
{
Bool _ exit = false; read configuration information recorded in android
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("Microsoftss", true );
SubkeyNames = aimdir. GetValueNames ();
Foreach (string keyName in subkeyNames)
{
If (keyName = name)
{
_ Exit = true;
Return _ exit;
}
}
Return _ exit;
}

For example, when we run Writevalue ("version", "v1.0.0.1") in the XP system, everything is OK! Then happy was thrown to the customer without thinking.

One day, the customer changed the win7 system and ran the software. The customer pulled the face of the No. 42 shoes and said, how can the software not run in win7 ..... The problem is what we mentioned above.

SolutionIt's a land ....

Several types are listed here.

First, Teach customers, or technical service personnel. This method does not need to modify our code, which is one of the advantages. Right-click .exe.

In the pop-up menu, select Properties, select compatibility, and select run this program as administrator. Easy to use

SecondIt's easy, but we needRegenerate Software. First, add the MANIFEST resource to the program, right-click the project, choose Properties> Security, and select enable ClickOnce security settings on the page.PropertiesAutomatically generated underApp. manifestFile.

The file content is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Asmv1: assembly manifestVersion = "1.0" xmlns = "urn: schemas-microsoft-com: asm. v1 "xmlns: asmv1 =" urn: schemas-microsoft-com: asm. v1 "xmlns: asmv2 =" urn: schemas-microsoft-com: asm. v2 "xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance ">
<AssemblyIdentity version = "1.0.0.0" name = "MyApplication. app"/>
<TrustInfo xmlns = "urn: schemas-microsoft-com: asm. v2">
<Security>
<RequestedPrivileges xmlns = "urn: schemas-microsoft-com: asm. v3">
<! -- UAC configuration options
If you want to change the Windows User Account Control level, replace it with one of the following nodes:
RequestedExecutionLevel node.

<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>
<RequestedExecutionLevel level = "requireAdministrator" uiAccess = "false"/>
<RequestedExecutionLevel level = "highestAvailable" uiAccess = "false"/>

If you want to use file and Registry virtualization to provide
For backward compatibility, delete the requestedExecutionLevel node.
-->
<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>
</RequestedPrivileges>
<ApplicationRequestMinimum>
<DefaultAssemblyRequest permissionSetReference = "Custom"/>
<PermissionSet class = "System. Security. PermissionSet" version = "1" Unrestricted = "true" ID = "Custom" SameSite = "site"/>
</ApplicationRequestMinimum>
</Security>
</TrustInfo>
</Asmv1: assembly>

Set <requestedExecutionLevel ="AsInvokerChange asInvoker of "uiAccess =" false "/>"RequireAdministrator". Recompile it and you will be OK.

See this!

This isUAC(User Account Control)

<! -- UAC configuration options

If you want to change the Windows User Account Control level, replace it with one of the following nodes:

RequestedExecutionLevel node.

 

<RequestedExecutionLevel level = "asInvoker" uiAccess = "false"/>

<Span> <requestedExecutionLevel level = "highestAvailable" uiAccess = "false"/>

 

If you want to use file and Registry virtualization to provide

For backward compatibility, delete the requestedExecutionLevel node.

-->

There are other methods. However, the above two methods are relatively simple ....

 

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.