Export (0) Print
Expand All

ServiceInstallerDialog Class

Provides a dialog box, which prompts for account information of a Windows Service application.

Namespace: System.ServiceProcess.Design
Assembly: System.ServiceProcess (in system.serviceprocess.dll)

public class ServiceInstallerDialog : Form
public class ServiceInstallerDialog extends Form
public class ServiceInstallerDialog extends Form

Normally, you do not use the ServiceInstallerDialog class directly within your code; this dialog is generally used by the installation utility for a Windows Service application.

If a ServiceProcessInstaller object has the Account set to User, and either the Username or Password is an empty string (""), then the installation utility uses ServiceInstallerDialog to prompt for these values.

ServiceInstallerDialog derives from Form; use the Form class methods and properties to display and manipulate the dialog. For example, use ShowDialog to display the form, and then check the Result before validating the input text for the Username and Password properties.

NoteNote

ServiceInstallerDialog does not check the credentials and privileges for the specified account. For example, the user might enter an invalid account name and password for the computer, or the account might not have the correct privileges to run as a service. Validate the user input to ensure that the installed service runs successfully.

The following example uses a ServiceInstallerDialog to prompt the user for a service installation account.

// Prompt the user for service installation account values.
public static bool GetServiceAccount(ref ServiceProcessInstaller svcInst)
{
    bool accountSet = false;
    ServiceInstallerDialog svcDialog = new ServiceInstallerDialog();

    // Query the user for the service account type.
    do
    {
        svcDialog.TopMost = true;
        svcDialog.ShowDialog();

        if (svcDialog.Result == ServiceInstallerDialogResult.OK)
        {
            // Do a very simple validation on the user
            // input.  Check to see whether the user name
            // or password is blank.

            if ((svcDialog.Username.Length > 0) &&
                (svcDialog.Password.Length > 0)   )
            {
                // Use the account and password.
                accountSet = true;

                svcInst.Account = ServiceAccount.User;
                svcInst.Username = svcDialog.Username;
                svcInst.Password = svcDialog.Password;
            }
        }
        else if (svcDialog.Result == ServiceInstallerDialogResult.UseSystem)
        {
            svcInst.Account = ServiceAccount.LocalSystem;
            svcInst.Username = null;
            svcInst.Password = null;
            accountSet  = true;
        }
            
        if (!accountSet )
        {
            // Display a message box.  Tell the user to
            // enter a valid user and password, or cancel
            // out to leave the service account alone.
            DialogResult result;
            result = MessageBox.Show("Invalid user name or password for service installation."+
                                     "  Press Cancel to leave the service account unchanged.",
                                     "Change Service Account", 
                                     MessageBoxButtons.OKCancel,
                                     MessageBoxIcon.Hand);

            if (result == DialogResult.Cancel)
            {
                // Break out of loop.
                break;
            }
        }
    } while (!accountSet);

    return accountSet;
}

// Prompt the user for service installation account values.
public static boolean GetServiceAccount(ServiceProcessInstaller svcInst)
{
    boolean accountSet = false;
    ServiceInstallerDialog svcDialog = new ServiceInstallerDialog();
    // Query the user for the service account type.
    do {
        svcDialog.set_TopMost(true);
        svcDialog.ShowDialog();

        if (svcDialog.get_Result().Equals(ServiceInstallerDialogResult.OK))
        {
            // Do a very simple validation on the user
            // input.  Check to see whether the user name
            // or password is blank.
            if (svcDialog.get_Username().get_Length() > 0 
                && svcDialog.get_Password().get_Length() > 0) {
                // Use the account and password.
                accountSet = true;

                svcInst.set_Account(ServiceAccount.User);
                svcInst.set_Username(svcDialog.get_Username());
                svcInst.set_Password(svcDialog.get_Password());
            }
        }
        else {
            if (svcDialog.get_Result().Equals(ServiceInstallerDialogResult.
                UseSystem)) {
                svcInst.set_Account(ServiceAccount.LocalSystem);
                svcInst.set_Username(null);
                svcInst.set_Password(null);
                accountSet = true;
            }
        }

        if (!(accountSet)) {
            // Display a message box.  Tell the user to
            // enter a valid user and password, or cancel
            // out to leave the service account alone.
            DialogResult result;
            result = MessageBox.Show("Invalid user name or password for "
                + "service installation." 
                + "  Press Cancel to leave the service account unchanged.", 
                "Change Service Account", 
                MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);

            if (result.Equals(DialogResult.Cancel)) {
                // Break out of loop.
                break;
            }
        }
    } while (!(accountSet));
    return accountSet;
} //GetServiceAccount

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ScrollableControl
           System.Windows.Forms.ContainerControl
             System.Windows.Forms.Form
              System.ServiceProcess.Design.ServiceInstallerDialog

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft