I am not sure if this is the answer to your question or not,but it seems to suppress the permissions.To make sure your application runs with administrator permission, add a new manifest
file from add new item menu.If everything goes correctly you should be seeing a new file named app.manifest
in the solution explorer.
When you've done it,open app.manifest
from Solution Explorer by double clicking it,when it opens in the code editor replace its trustinfo
section with this;
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- 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="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel node will disable file and registry virtualization.
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
What we did is,we changed the application's manifest to make sure it gets administrative right's while running.
This was to illustrate,how to run the application as administrator everytime,but instead if your need is to only check if the application has administrative rights,you can check it in this way;
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal appprincipal = (WindowsPrincipal) Thread.CurrentPrincipal;
if(appprincipal.IsInRole("Administrators"))//Check if the current user is admin.
{
//Yeah,the app has adminstrative rights,do what you need.
}
else
{
//Not running as administrator,react as needed.
//Show error message.
}
Update
To get the full path of current application, use this;
string path=Application.ExecutablePath;
Application.ExecutablePath returns the absolute path of the exceutable which is calling the method.
For Ex : If you application is Notepad,it would return C:\Windows\System32\Notepad.exe.
Hope it solves the issue.