Win7 has finally been released
When UAC is enabled, the execution of programs in win7 is performed in a way with low permissions by default, but in this way, some operations will fail (such as modifying the registry, listening port, writing files to the system directory, etc.). To perform these operations, we need to execute the program as an administrator.
Of course, you only need to right-click the program and select "Run as administrator". However, manifest is required to automatically run the program as administrator.
First, we will create a new project (if you are too lazy to change the name, it will be windowsformsapplication1)
Press F5 to execute the command (well, it seems that there is no problem [empty document, it's strange if there is a problem...])
Then add manifest (the Chinese version is called "Application List file ")
Next let's take a look at the manifest content.
<? 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-Micros Oft-com: ASM. V3 "> <! -- UAC configuration option if you want to change the Windows User Account Control level, replace the requestedexecutionlevel node with one of the following nodes. <Strong level = "asinvoker" UIAccess = "false"/> <requestedexecutionlevel = "requireadministrator" UIAccess = "false"/> <requestedexecutionlevel = "highestavailable" UIAccess = "false" /> if you want to use file and Registry virtualization to provide backward compatibility, delete the requestedexecutionlevel node. --> <Requestedexecutionlevel level = "asinvoker" UIAccess = "false"/> </requestedprivileges> </Security> </trustinfo> </asmv1: Assembly>
The description in the content is detailed enough. As long as we replace asinvoker with requireadministrator, our program will require the Administrator permission to run by default. Then, we should try the following execution.
Well, the window pops up.
Check the program icon:
Success...
Next let's talk about how to add a small shield icon to the buttons of the program.
So we need to call Win32 API.
Do you want to call an API? You need to reference the namespace first.
using System.Runtime.InteropServices;
Then call the API
[DllImport("user32.dll")] private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); /////////////////////////////////////////////////////////////////////// /// <summary> /// Enables the elevated shield icon on the given button control /// </summary> /// <param name="ThisButton"> /// Button control to enable the elevated shield icon on. /// </param> /////////////////////////////////////////////////////////////////////// private void EnableElevateIcon_BCM_SETSHIELD(Button ThisButton) { // Input validation, validate that ThisControl is not null if (ThisButton == null) { return; } // Define BCM_SETSHIELD locally, declared originally in Commctrl.h uint BCM_SETSHIELD = 0x0000160C; // Set button style to the system style ThisButton.FlatStyle = FlatStyle.System; // Send the BCM_SETSHIELD message to the button control SendMessage(new HandleRef(ThisButton, ThisButton.Handle), BCM_SETSHIELD, new IntPtr(0), new IntPtr(1)); }
Drag a button on the form to make it bigger. The icon cannot be seen clearly.
Then, add the icon to button1 using the API in form1_load.
private void Form1_Load(object sender, EventArgs e) { EnableElevateIcon_BCM_SETSHIELD(button1); }
Finally, let's take a look at the effect!
Well? Why is shield a little different? The icon above is on server08, And the icon on win7 should be as follows:
If there is any error, please note it.