I'm currently using this code to restart app with admin rights.

// If application can not write to path, alert user.
            if (!pathiswritable)
            {

                if (MessageBox.Show("The application cannot use the current location\nRestart as administrator?", 
                    "Need elevation", 
                    MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    Exit(ExitError.NOT_WRITABLE_USER);
                }

                ProcessStartInfo SelfProc = new ProcessStartInfo
                {
                    UseShellExecute = true,
                    WorkingDirectory = Environment.CurrentDirectory,
                    FileName = Application.ExecutablePath,
                    Verb = "runas"
                };

                try
                {
                    Process.Start(SelfProc);
                    Environment.Exit(0);
                }
                catch(Exception)
                {
                    Exit(ExitError.NO_ELEVATE);
                }
            }

But I'd like to make it clear with that admin shield icon on the yes button, that will restart as admin.

Wondering if there is a .net solution for this, or do I need to use unmanaged code?

Recommended Answers

All 10 Replies

You cannot change much on a messagebox.
But could just write your own!
Take this snippet as a start.

Ah yes, silly me.

Thanks.

Does anyone know if there is a .net way to add the admin icon to a form button?

A Button has Image and ImageAlign properties.

I tried to make a new form to emulate a MessagBox and now vs2010 keeps crashing.

I created the form, then when I tried to click on the design view docked tab, VS ran my app and crashed, now I cannot look at any design view of any form, as when I double click on any form in solution explorer, VS just runs the application and crashes.

Also it decided to add a couple of buttons and a menustrip to the form for some reason, and they cannot be removed and all properties of them are greyed out and do not have any code to show they are really there in code view.

After a search it was said by a few that git had caused such a problem, so I first tried disabling git, then uninstalling it, both never solved the problem.

Here's a pich of whay it did to my form in designer, I got this image the one time it let me view it.

http://s28.postimg.org/yj4bj9u59/vs2010crash01.png

There was just 2 buttons and two labels (one which I added the image to) on my form, an extra button and menustrip was phantomly added.

It's all very weird, I've never had anything like this before.

Of course it might just have been a coincidence that I made that new form with the settings from that link.

But I've tried, rebooting computer, rebooting clean boot, running VS2010 in safe mode, deleting suo file, etcetera.

The problem persists with only this project, I think somehow the files have become corrupt, is there a way to restore them?

I don't mean the code files, rather the other files VS generates.

(edit) also tried resetting the VS reset all settings.

I should also add that if I run the app from VS
(when it open without running the code (yes it's doing that now too))
It runs fine and everything seems as it should.

Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
Stack:
at Microsoft.Internal.VisualStudio.PlatformUI.Utilities.SetValue(Microsoft.VisualStudio.Shell.Interop.IVsUIDataSource, System.String, Microsoft.VisualStudio.Shell.Interop.IVsUIObject)
at Microsoft.Internal.VisualStudio.PlatformUI.DataSource.SetValue(System.String, System.Object)
at Microsoft.VisualStudio.PlatformUI.MainWindowDataSource.set_IsClosed(Boolean)
at Microsoft.VisualStudio.PlatformUI.MainWindow+DataSourceEventSink.Disconnect(Microsoft.VisualStudio.Shell.Interop.IVsUISimpleDataSource)
at Microsoft.Internal.VisualStudio.PlatformUI.DataSource.<DisconnectEventSubscribers>b__6(UInt32, Microsoft.VisualStudio.Shell.Interop.IVsUIDataSourcePropertyChangeEvents)
at Microsoft.Internal.VisualStudio.PlatformUI.CookieTable3[[System.UInt32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b88a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b88a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b88a5c561934e089]].ForEach(Microsoft.Internal.VisualStudio.PlatformUI.CookieTableCallback2<UInt32,System.__Canon>, Boolean)
at Microsoft.Internal.VisualStudio.PlatformUI.DataSource.DisconnectEventSubscribers()
at Microsoft.Internal.VisualStudio.PlatformUI.DataSource.Dispose(Boolean)
at Microsoft.Internal.VisualStudio.PlatformUI.DataSource.Finalize()

Did you consider malware or virus?
I should try to copy all the source files and move them to a new project, to seeif that works.(No garantee btw)
You probably cannot delete the folder containing this project either.

I've considered malware of some sort, but highly doubt it on the basis that besides targeting this one particular project file there are absolutely no indications on my system or browsing experience that malware is present.

I will transfere my code to another project, and I'm confident it will be fine, but this is going to be my template and I'd sooner know the cause so as to avoid it in the future rather than just treat the symptoms and be done.

Thank you for your input ddanbe, I appreciate it.

I was wrong.

Same thing happens when I try to add the new form.

here are the codes of the form, maybe someone can see the problem.

I can double click on this and the designer will appear. but when I try to do same with form1 (main form) VS runs part of the app and crashes. Only when this form is added. to the project.

If I deletes it, it's too late, the problem remains.

(edit) I should add, that the following code it not even used in my project yet, meaning a class instance is never created, just the mere code being in the project seems to cause the problem.

AdminConfirmForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppBase
{
    public partial class AdminConfirmForm : Form
    {
        public AdminConfirmForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Global.adminconfirmed = true;
            this.Close();
        }
    }
}

AdminConfirmForm.Designer.cs

namespace AppBase
{
    partial class AdminConfirmForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Image = global::AppBase.Properties.Resources.uac100;
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(110, 144);
            this.label1.TabIndex = 0;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(152, 13);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(240, 74);
            this.label2.TabIndex = 1;
            this.label2.Text = "Application cannot write to current folder\n\nRestart as aministrator?";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(155, 102);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(98, 52);
            this.button1.TabIndex = 2;
            this.button1.Text = "Ok";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(292, 102);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(100, 52);
            this.button2.TabIndex = 3;
            this.button2.Text = "Cancel";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // AdminConfirmForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(404, 166);
            this.ControlBox = false;
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "AdminConfirmForm";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Application needs elevation";
            this.TopMost = true;
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.