본문 바로가기
개발/.NET

윈도우 서비스 만들기

by 그저그런보통사람 2010. 4. 20.
    [RunInstaller(true)] 
   
public class TestInstaller : Installer
   
{
       
private ServiceInstaller serviceInstaller;
       
private ServiceProcessInstaller serviceProcessInstaller;
       
public OregonDatabaseWinServiceInstaller()
       
{
                serviceInstaller
= new ServiceInstaller();
                serviceInstaller
.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
                serviceInstaller
.ServiceName = "Test";
                serviceInstaller
.DisplayName = "Test Service";
                serviceInstaller
.Description = "Test";
                serviceInstaller
.StartType = ServiceStartMode.Automatic;
               
Installers.Add(serviceInstaller);
                serviceProcessInstaller
= new ServiceProcessInstaller();
                serviceProcessInstaller
.Account = ServiceAccount.User;  
               
Installers.Add(serviceProcessInstaller);
       
}
       
public string GetContextParameter(string key)
       
{
               
string sValue = "";
               
try
               
{
                        sValue
= this.Context.Parameters[key].ToString();
               
}
               
catch
               
{
                        sValue
= "";
               
}
               
return sValue;
       
}
 
       
// Override the 'OnBeforeInstall' method.
       
protected override void OnBeforeInstall(IDictionary savedState)
       
{
               
base.OnBeforeInstall(savedState);
               
string username = GetContextParameter("user").Trim();
               
string password = GetContextParameter("password").Trim();
               
if (username != "")
                        serviceProcessInstaller
.Username = username;
               
if (password != "")
                        serviceProcessInstaller
.Password = password;
       
}
   
}