If you are using Windows 7 or Windows Vista, or Windows Server 2008, you should know the UAC (User Account Control), this is one of significant differences between Windows XP and the newer Windows System.
“User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. When you attempt to perform a task that requires administrative privileges, you can dynamically elevate your role by using the Consent dialog box. The code that executes the IsInRole method does not display the Consent dialog box. The code returns false if you are in the standard user role or in the Built-in Administrators group. You can elevate your privileges before you execute the code by right-clicking the application icon and indicating that you want to run as an administrator.” – From MSDN.
When you build a Windows Forms application using C#, your application will not run as administrator by default. Then if your application need more permissions just like an Administrator, you might get problem since your application can not access enough resources;
The simplest solution is disabling the UAC on your Windows system. About how to disable UAC, we will discuss in another time, this time we do not want to talk about more about this since disabling UAC for a windows system is not recommended.
The normal and the best solution is using manifest file.
Open your application in Visual Studio 2010, Right click the project and add a new item, choose “Application Manifest File”:
Now you can see a new file named “app.manifest” is added under Properties folder:
When you open app.manifest file, you can see there are some sample settings have been already there, for example:
<requestedPrivileges xmlns=<span class="str">"urn:schemas-microsoft-com:asm.v3"</span>> <!-- UAC Manifest Options If you want to change the Windows User Account Control level replace the requestedExecutionLevel node with one of the following. <requestedExecutionLevel level=<span class="str">"asInvoker"</span> uiAccess=<span class="str">"false"</span> /> <requestedExecutionLevel level=<span class="str">"requireAdministrator"</span> uiAccess=<span class="str">"false"</span> /> <requestedExecutionLevel level=<span class="str">"highestAvailable"</span> uiAccess=<span class="str">"false"</span> /> Specifying requestedExecutionLevel node will disable file and registry virtualization. If you want to utilize File and Registry Virtualization <span class="kwrd">for</span> backward compatibility then delete the requestedExecutionLevel node. --> </requestedPrivileges>
What we should do is just remove comment for this line:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
When you done above steps, rebuild your windows form application, then every time when you try to restart your application, you will be asked whether you need to run as administrator.