1

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)?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • [Run the application as a administrator and before it starts check if it have admin rights?](http://stackoverflow.com/a/4641029/342740) – Prix Sep 14 '13 at 16:37

3 Answers3

0

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.

devavx
  • 1,035
  • 9
  • 22
  • Thank you very much it did resolved my issue :D one more thing if i jst wants the exe file which makes it run u have any idea how to do that? – Waqar Muhammad Sep 15 '13 at 01:54
  • @WaqarMuhammad You mean the application's executable (the current file which is doing all the stuff) ? – devavx Sep 15 '13 at 01:57
  • yes that executable file which is running my main application – Waqar Muhammad Sep 16 '13 at 11:25
  • this is my issue :P http://stackoverflow.com/questions/18808657/how-to-filter-between-junk-exe-files-and-workable-exe-files – Waqar Muhammad Sep 16 '13 at 13:18
  • I dont want to get exe file's path when user opens it i want all the application's main exe file paths w/o opening applications check the link i stated above – Waqar Muhammad Sep 16 '13 at 13:28
  • @WaqarMuhammad OK,so you want all the .`exe files` from **a directory**,and the directory will be chosen by the user by some speech command.If this is the case reply me and I'll try to make something. – devavx Sep 16 '13 at 16:30
  • no i want that user dont have to specify any directory when my software installs on any machine it will get all installed program's executable file(only those executable files which are main to run the program) like there are 3 exe files in Internet explorer folders C:\Program Files\Internet Explorer\ieinstal.exe C:\Program Files\Internet Explorer\ielowutil.exe C:\Program Files\Internet Explorer\iexplorer.exe but on my needs i jst want iexplorer.exe bcz its the file which runs the main program i want to filter this file from those 2 and enter it in any notepad file along with – Waqar Muhammad Sep 16 '13 at 17:47
  • main window title and its path – Waqar Muhammad Sep 16 '13 at 17:49
0

You need to have administrative rights to access root drive. You can use the below code to test if your app has Administrator rights.

    static void Test()
    {
        if (!HasAdministratorRights())
        {
            // throw error to notfying that the app need admin rights
        }

        // Get files for 
    }

    private static bool HasAdministratorRights()
    {
        var currentIdentity = WindowsIdentity.GetCurrent();
        if (currentIdentity == null)
            return false;

        return new WindowsPrincipal(currentIdentity)
            .IsInRole(WindowsBuiltInRole.Administrator);
    }
Vinod Kumar Y S
  • 628
  • 3
  • 9
0

If you have any account with admin right then you can run that code snippet using impersonation.

Here is the link of implementation

Community
  • 1
  • 1
Thilina H
  • 5,754
  • 6
  • 26
  • 56