Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / Win32

Application Management

Rate me:
Please Sign up or sign in to vote.
3.71/5 (5 votes)
18 Feb 2010Ms-PL3 min read 27.4K   649   48  
Application Management makes your application's life easier. It will automatically do memory management, handle and log unhandled exceptions, profile your functions, make your application single instance, and also provide utility functions to get system information.
using System;
using System.Diagnostics;
using System.Security.Principal;

namespace ApplicationManagement
{
    /// <summary>
    /// Functions relative to Windows Vista and/or above.
    /// </summary>
    public static class VistaSecurity
    {
        #region IsAdmin
        /// <summary>
        /// Check if current user is an administrator.
        /// </summary>
        /// <returns>Returns true if user is an administrator, otherwise false.</returns>
        public static bool IsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal p = new WindowsPrincipal(id);
            return p.IsInRole(WindowsBuiltInRole.Administrator);
        }
        #endregion

        #region RestartElevated

        /// <summary>
        /// Restart application with elevated permitions.
        /// </summary>
        /// <param name="app">Path to application executable, use: Application.ExecutablePath</param>
        /// <returns>Returns true if all went fine, otherwise false.</returns>
        /// <remarks>You need kill your application if this returns true, be call: Application.Exit();</remarks>
        /// <example>
        /// if(!VistaSecurity.IsAdmin())
        /// {
        ///     if(VistaSecurity.RestartElevated(Application.ExecutablePath))
        ///         Application.Exit();
        /// }
        /// </example>
        public static bool RestartElevated(string app)
        {
            using (Process proc = new Process())
            {
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                proc.StartInfo.FileName = app;
                proc.StartInfo.Verb = "runas";
                try
                {
                    proc.Start();
                    proc.Close();
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    return false;
                }
            }
            return true;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions