Link to home
Create AccountLog in
Avatar of deanvanrooyen
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("myservice.exe");
                servicepath = localByName[0].FileName + "log.txt";
            }
            catch (Exception e)
            {
                servicepath = "c:\\myservice.exe.log.txt";
            }
thanks!
Avatar of deanvanrooyen
deanvanrooyen

ASKER

opps

       Process[] localByName = Process.GetProcessesByName("CALLMASTER.ASTERISK.exe");
                servicepath = localByName[0].MainModule.FileName + "log.txt";
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
it is a bit messy

        public static void LogError(string s)
        {
            string servicepath;
            //get process
            try
            {

                System.Diagnostics.Process localByName = System.Diagnostics.Process.GetCurrentProcess();
                servicepath = localByName.MainModule.FileName + "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();
            }

        }
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
      }
}
hi Greg,

ActivationContext ac = AppDomain.CurrentDomain.ActivationContext;
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.ServiceBase, I thought of application object not a chance that i could see,

thanks for the info!
where is the null reference?
for example

            System.Windows.Forms.MessageBox.Show("onstart", "caption", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            System.Windows.Forms.MessageBox.Show(AppDomain.CurrentDomain.ActivationContext.Identity.FullName, "caption", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);

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
this
                WriteError.LogError("AppDomain : " + AppDomain.CurrentDomain.ToString());
                WriteError.LogError("ActivationContext : " + AppDomain.CurrentDomain.ActivationContext.ToString());
                WriteError.LogError("ApplicationIdentity : " + AppDomain.CurrentDomain.ActivationContext.Identity.ToString());

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.