I am trying to get path of all EXE files present on my C drive. I am facing an issue which is nothing more than access problem due to lack of administrative rights.
I wrote this code,but the system is denying access to those files.
DriveInfo drive = new DriveInfo(@"C:\\");
foreach (DirectoryInfo dir in drive.RootDirectory.GetDirectories(".*exe",SearchOption.AllDirectories))
{
path.Add(dir.ToString());
}
How can I get Windows to ask the user to elevate permissions to administrative (the shield/dark screen message)?
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 namedapp.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 itstrustinfo
section with this;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;
Update
To get the full path of current application, use this;
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.