Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / Visual Basic

Certification by Example

Rate me:
Please Sign up or sign in to vote.
4.93/5 (95 votes)
8 Jan 2008CPOL55 min read 185.2K   714   233  
How to prepare a .NET application to obtain the Certified for Windows Vista logo, including the source code (Visual Studio 2005 solution) of a simple but complete application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System.DirectoryServices;

namespace Capsule.KillerApplication.Install
{
    /// <summary>
    /// This class contains custom actions for application install and uninstall.
    /// In the installer project, add this assembly to "Common Files Folder".
    /// Also, set the results of this project in the custom actions window,
    /// under "Install", "Rollback", and "Uninstall".
    /// </summary>
    [RunInstaller(true)]
    public partial class KillerApplicationInstaller:Installer
    {
        private const string APPNAME="Killer Application";
        private const string APPFILE="KILLER APPLICATION.EXE";

        private string ApplicationDataPath
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), APPNAME); }
        }

        public KillerApplicationInstaller()
        {
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            //* Create the event source

            if(!EventLog.SourceExists(APPNAME))
            {
                EventLog.CreateEventSource(APPNAME, "Application");
            }

            //* Give normal users full access to the data directory

            string userGroupName=FindUserForSid.GetNormalUsersGroupName();
            AclManager manager=new AclManager(ApplicationDataPath, userGroupName, "F");
            manager.SetAcl();

            base.Install(stateSaver);
        }

        public override void Rollback(System.Collections.IDictionary savedState)
        {
            if(EventLog.SourceExists(APPNAME))
            {
                EventLog.DeleteEventSource(APPNAME);
            }
            base.Rollback(savedState);
        }

        protected override void OnAfterUninstall(System.Collections.IDictionary savedState)
        {
            //* Delete data files

            string path=ApplicationDataPath;
            if(Directory.Exists(path))
            {
                try
                {
                    Directory.Delete(path, true);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(string.Format(
@"Error when trying to delete the program data folder:
({0}) {1}

Uninstall process will continue, but the folder will not be deleted.
The folder path is:
{2}", ex.GetType().Name, ex.Message, path), APPNAME + " uninstaller", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            //* Delete the event source

            if(EventLog.SourceExists(APPNAME))
            {
                EventLog.DeleteEventSource(APPNAME);
            }

            //* Delete file from Prefetch folder
            
            string prefetchPath=Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "Prefetch");

            try
            {
                string[] files=Directory.GetFiles(prefetchPath, APPFILE+"*.*");
                foreach(string file in files)
                {
                    File.Delete(file);
                }
            }
            catch
            {
                string prefetchDir=Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "Prefetch");
                MessageBox.Show(@"Some "+APPNAME+" files may remain in the "+prefetchPath+" directory. You can delete these files manually.",
                    APPNAME + " uninstaller", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            base.OnAfterUninstall(savedState);
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer SunHotels
Spain Spain
Under the secret identity of a C# programmer, a freaky guy who loves MSX computers and japanese culture is hidden. Beware!

Comments and Discussions