deanvanrooyen
asked on
get windows service execution path
hi,
i have built a windows service - all fine.
i need to get the the execition path of the service, is there a better way then this? this is called from within the service class,i could also use getcurrentprocess
try
{
ProcessModule[] localByName = Process.GetProcessesByName ("myservic e.exe");
servicepath = localByName[0].FileName + "log.txt";
}
catch (Exception e)
{
servicepath = "c:\\myservice.exe.log.txt ";
}
thanks!
i have built a windows service - all fine.
i need to get the the execition path of the service, is there a better way then this? this is called from within the service class,i could also use getcurrentprocess
try
{
ProcessModule[] localByName = Process.GetProcessesByName
servicepath = localByName[0].FileName + "log.txt";
}
catch (Exception e)
{
servicepath = "c:\\myservice.exe.log.txt
}
thanks!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
it is a bit messy
public static void LogError(string s)
{
string servicepath;
//get process
try
{
System.Diagnostics.Process localByName = System.Diagnostics.Process .GetCurren tProcess() ;
servicepath = localByName.MainModule.Fil eName + "log.txt";
StreamWriter sw = new StreamWriter(servicepath, true);
sw.WriteLine(DateTime.Now. ToString(" yyyyMMdd HH:mm:ss") + " - " + s);
sw.Close();
}
catch (Exception e)
{
servicepath = "c:\\myservice.exe.log.txt ";
StreamWriter sw = new StreamWriter(servicepath, true);
sw.WriteLine(DateTime.Now. ToString(" yyyyMMdd HH:mm:ss") + " - " + s);
sw.Close();
}
}
public static void LogError(string s)
{
string servicepath;
//get process
try
{
System.Diagnostics.Process
servicepath = localByName.MainModule.Fil
StreamWriter sw = new StreamWriter(servicepath, true);
sw.WriteLine(DateTime.Now.
sw.Close();
}
catch (Exception e)
{
servicepath = "c:\\myservice.exe.log.txt
StreamWriter sw = new StreamWriter(servicepath, true);
sw.WriteLine(DateTime.Now.
sw.Close();
}
}
that is less likely to fail that your first example though .. your first example would have had some troubles if there was more than 1 instance running.
I would also not do that everytime you log an error.
public class Logger {
static string Filename;
public static void LogError(string s){
StreamWriter sw = new StreamWriter(filename, true);
sw.WriteLine(DateTime.Now. ToString(" yyyyMMdd HH:mm:ss") + " - " + s);
sw.Close();
}
public Logger() {
save the name of your file to filename
}
}
I would also not do that everytime you log an error.
public class Logger {
static string Filename;
public static void LogError(string s){
StreamWriter sw = new StreamWriter(filename, true);
sw.WriteLine(DateTime.Now.
sw.Close();
}
public Logger() {
save the name of your file to filename
}
}
ASKER
hi Greg,
ActivationContext ac = AppDomain.CurrentDomain.Ac tivationCo ntext;
ApplicationIdentity ai = ac.Identity;
Console.WriteLine("Full name = " + ai.FullName);
Console.WriteLine("Code base = " + ai.CodeBase);
errors out with a null ref object...
must be slightly different with System.ServiceProcess.Serv iceBase, I thought of application object not a chance that i could see,
thanks for the info!
ActivationContext ac = AppDomain.CurrentDomain.Ac
ApplicationIdentity ai = ac.Identity;
Console.WriteLine("Full name = " + ai.FullName);
Console.WriteLine("Code base = " + ai.CodeBase);
errors out with a null ref object...
must be slightly different with System.ServiceProcess.Serv
thanks for the info!
where is the null reference?
ASKER
for example
System.Windows.Forms.Messa geBox.Show ("onstart" , "caption", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Bu tton1, MessageBoxOptions.ServiceN otificatio n);
System.Windows.Forms.Messa geBox.Show (AppDomain .CurrentDo main.Activ ationConte xt.Identit y.FullName , "caption", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Bu tton1, MessageBoxOptions.ServiceN otificatio n);
the first message box pops but the second doesnt and the exception caught is
Object reference not set to an instance of an object.
this must be that there is no application context with the windows service.
System.Windows.Forms.Messa
System.Windows.Forms.Messa
the first message box pops but the second doesnt and the exception caught is
Object reference not set to an instance of an object.
this must be that there is no application context with the windows service.
should be fairly easy to see what is null there .. which field is actually null? ActivationContext or Identity
ASKER
this
WriteError.LogError("AppDo main : " + AppDomain.CurrentDomain.To String());
WriteError.LogError("Activ ationConte xt : " + AppDomain.CurrentDomain.Ac tivationCo ntext.ToSt ring());
WriteError.LogError("Appli cationIden tity : " + AppDomain.CurrentDomain.Ac tivationCo ntext.Iden tity.ToStr ing());
gives
20060910 12:12:49 - AppDomain : Name:myservice.exe
There are no context policies.
20060910 12:12:49 - OnStart : Object reference not set to an instance of an object.
looks like
ActivationContext
"An ActivationContext object that represents the activation context for the current application domain, or a null reference (Nothing in Visual Basic) if the domain has no activation context. "
so i can only deduce that activation context is null, i even tried it in a simple winform and still null, this must require some extra setting up.
I am going to leave it at that for now, thanks.
WriteError.LogError("AppDo
WriteError.LogError("Activ
WriteError.LogError("Appli
gives
20060910 12:12:49 - AppDomain : Name:myservice.exe
There are no context policies.
20060910 12:12:49 - OnStart : Object reference not set to an instance of an object.
looks like
ActivationContext
"An ActivationContext object that represents the activation context for the current application domain, or a null reference (Nothing in Visual Basic) if the domain has no activation context. "
so i can only deduce that activation context is null, i even tried it in a simple winform and still null, this must require some extra setting up.
I am going to leave it at that for now, thanks.
ASKER
Process[] localByName = Process.GetProcessesByName
servicepath = localByName[0].MainModule.