image

Buenas,

the answer to the question:

Is it possible to launch an application deployed with ClickOnce with administrator permissions?

It has a very simple answer:

NOT

The solution is two paragraphs later, so you can save the following text. The correct way to define an application requires certain permissions is through a manifesto. In it, you can indicate everything that requires an application at run time. The step by step, is on MSDN and is described in:

The problem is that if we have an application that then we deploy with ClickOnce, we apply this change in the manifest, and then try to compile we find a beautiful

image

Error 1 ClickOnce does not support the request execution level ‘requireAdministrator’.

And now? as one of the solutions comes from the hand of this post , where what is being done is basically to check if the application is launched with ADMIN and otherwise permits, launch the application again with the appropriate permissions.

I have created a small class that in addition to applying this process displays a message warning that the application will be launched again.

   1: public class RunAsAdministrator

   2: {

   3:     public bool IsRunAsAdministrator()

   4:     {

   5:         var windowsIdentity = WindowsIdentity.GetCurrent();

   6:         var windowsPrincipal = new WindowsPrincipal(windowsIdentity);

   7:         return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);

   8:     }

   9:  

  10:     public static void ValidateAdministratorModeAndRestart(bool displayMessage)

  11:     {

  12:  

  13:         var runAsAdministrator = new RunAsAdministrator();

  14:         if (runAsAdministrator.IsRunAsAdministrator()) return;

  15:  

  16:  

  17:         if (displayMessage)

  18:         {

  19:             var fi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

  20:             string productName = fi.FileDescription;

  21:             MessageBox.Show(string.Format("{0} will launch itself with admin privileges", productName));

  22:         }

  23:  

  24:         var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)

  25:                               {

  26:                                   UseShellExecute = true,

  27:                                   Verb = "runas"

  28:                               };

  29:         try

  30:         {

  31:             Process.Start(processInfo);

  32:         }

  33:         catch (Exception)

  34:         {

  35:             MessageBox.Show("Sorry, this application must be run as Administrator.");

  36:         }

  37:         Application.Current.Shutdown();

  38:     }

  39: }

In my case it was enough and with some exceptions that occur to me from memory, I think it may be enough Open-mouthed smile

Source: http://antscode.blogspot.com.es/2011/02/running-ClickOnce-application-as.html

Saludos @ Home

El Bruno

image image image

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading