3

I realise something that when I restart my application (with code), it does not restart with admin rights. When I start my app by double clicking, it ask me that you want to run it as an admin and I say yes. But after that I need to restart my application programmatically and it has to restart as also admin but it does not.

I use simply this code to restart

        Application.Restart();

and I added the following line to my app manifest file

  <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" /> 

Edit: I am not looking for using Progress class to restart. can I do it with Application.Restart?

We made a management console and we have plugins and plugins are needed to be updated. Thats why I have to delete the old dlls to upgrade but I receive errors when I try and I believe it is because the program does not have administrator permissions.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
ertan2002
  • 1,458
  • 1
  • 32
  • 68
  • I can not reproduce your problem. I made [a basic application with a button that restarts](https://gist.github.com/leftler/185be29579275fb716ff6ec42fa0110c). The title remains true even after restarting so the application is running as an administrator. I tried with both `requireAdministrator` and with `asInvoker` with a right click, run as administrator. Both methods remained elevated after restarting. – Scott Chamberlain Aug 29 '17 at 14:31
  • Can you do like I did and create a [Minimal Complete Verifiable example](https://stackoverflow.com/help/mcve) that we could copy and paste and run to see the problem? I would be willing to re-open the question if you could post a way for us to reproduce the problem. – Scott Chamberlain Aug 29 '17 at 14:35
  • @ScottChamberlain, thank you for your reply. I tried your code and yes I got as true but I made a setup file and we installed it into the programfiles and i need to delete some files in my application folder. It gives me an authorization problem. We think that its about the admin rights. Because when i do that in my debug folder, it can delete it. – ertan2002 Aug 29 '17 at 14:40
  • 1
    You should not be adding or deleting files in the application folder once it is installed. Any files that involve modification should be stored in `%ProgramData%` instead of `%ProgramFiles%` – Scott Chamberlain Aug 29 '17 at 14:45
  • @ScottChamberlain, We made a management console and we have plugins and plugins are needed to be updated. Thats why I have to delete the old dlls to upgrade.. – ertan2002 Aug 29 '17 at 14:47
  • The plugins can still be in `%ProgramData%\YourAppName\Plugins` just add a [`AppDomain.CurrentDomain.AssemblyResolve += YourResolver`](https://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve(v=vs.110).aspx) to choose the folder to look in for the missing assemblies. – Scott Chamberlain Aug 29 '17 at 14:48
  • @ScottChamberlain, thank you for your reply but we already completed the system. I cannot change it now easily. I have to find a solution for program files or i should install the app somewhere else. – ertan2002 Aug 29 '17 at 14:53
  • What you can do is what Steam does for it's games. In your installer set the permissions of the folder the plugins live in to allow regular users to add or remove to that folder. Then plugins should work without worrying about permissions. – Scott Chamberlain Aug 29 '17 at 14:57
  • 1
    However it sounds like you have a XY Problem. You should have asked about why you can't delete the dlls, I don't think it is a permissions problem, I think it is a file in use problem. One thing you can do to work around that problem is load the DLLs using Shadow Copy. you either need a 2nd app domain you can unload that holds the DLLs or you need to start the program with Shadow Copy DLLs, See this answer of mine https://stackoverflow.com/questions/30487463/how-can-you-modify-an-in-use-a-dll/30489982#30489982 – Scott Chamberlain Aug 29 '17 at 14:58
  • Thank you so much your replies. I saw the exception that says an unauhorization problem. but interestingly I can do that in my computer. (we've tested in my friend's computer). I will deeply check it. Thanx a lot again – ertan2002 Aug 29 '17 at 15:01

0 Answers0