/// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            //Look for V4V device before we try to start the service
            ManagementObjectSearcher deviceList = new ManagementObjectSearcher("Select * from Win32_PnPEntity");
            bool v4vfound = false;
            if (deviceList != null)
            {
                // Enumerate the devices
                foreach (ManagementObject device in deviceList.Get())
                {
                    if (device.GetPropertyValue("DeviceID").ToString().StartsWith("XEN\\V4V"))
                    {
                        // Evaluate the device status.
                        string status = device.GetPropertyValue("Status").ToString();
                        bool working = ((status == "OK") || (status == "Degraded") || (status == "Pred Fail"));
                        if (working) v4vfound = true;
                    }
                }
            }
            #if DEBUG
            bool bService = true;
            foreach (string arg in args)
            {
                if (arg.ToLower() == "/console")
                {
                    bService = false;
                    break; //We only care about /console, all other args go unloved
                }
            }

            //Running as a service, not console
            if (bService)
            {
            #endif
                if (!v4vfound)
                {
                    EventLog.WriteEntry("XenClientGuestService", "Not starting XenClientGuestService, V4V device is unavailable");
                }
                else
                {
                    // System.ArgumentException only occurs if null passed, constructor can not return null
                    // System.ComponentModel.Win32Exception does not happen in .NET 3.5 & above, the tooltip box lies
                    ServiceBase ServiceToRun = new XenClientGuestService();
                    ServiceBase.Run(ServiceToRun);
                }
            #if DEBUG
            }
            else
            {
                //No exception handling here as to be honest, if any of this breaks we have serious problems & it's for debugging purposes only!
                String calledFrom = System.IO.Directory.GetCurrentDirectory();
                String executedFrom = System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
                System.Console.WriteLine("Running executable from {0}", calledFrom);

                if (!calledFrom.Equals(executedFrom))
                {
                    System.Console.WriteLine("Executable actually running in {0}", executedFrom);
                    System.IO.Directory.SetCurrentDirectory(executedFrom);
                }

                XenClientGuestService service = new XenClientGuestService();
                service.OnServiceStart(args);

                System.Console.WriteLine("Press enter to end XenClientGuestService...");
                System.Console.ReadLine();

                service.OnServiceStop();
            }
            #endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            //Look for V4V device before we try to start the service
            ManagementObjectSearcher deviceList = new ManagementObjectSearcher("Select * from Win32_PnPEntity");
            bool v4vfound = false;

            if (deviceList != null)
            {
                // Enumerate the devices
                foreach (ManagementObject device in deviceList.Get())
                {
                    if (device.GetPropertyValue("DeviceID").ToString().StartsWith("XEN\\V4V"))
                    {
                        // Evaluate the device status.
                        string status  = device.GetPropertyValue("Status").ToString();
                        bool   working = ((status == "OK") || (status == "Degraded") || (status == "Pred Fail"));
                        if (working)
                        {
                            v4vfound = true;
                        }
                    }
                }
            }
#if DEBUG
            bool bService = true;
            foreach (string arg in args)
            {
                if (arg.ToLower() == "/console")
                {
                    bService = false;
                    break; //We only care about /console, all other args go unloved
                }
            }

            //Running as a service, not console
            if (bService)
            {
#endif
            if (!v4vfound)
            {
                EventLog.WriteEntry("XenClientGuestService", "Not starting XenClientGuestService, V4V device is unavailable");
            }
            else
            {
                // System.ArgumentException only occurs if null passed, constructor can not return null
                // System.ComponentModel.Win32Exception does not happen in .NET 3.5 & above, the tooltip box lies
                ServiceBase ServiceToRun = new XenClientGuestService();
                ServiceBase.Run(ServiceToRun);
            }
#if DEBUG
        }

        else
        {
            //No exception handling here as to be honest, if any of this breaks we have serious problems & it's for debugging purposes only!
            String calledFrom   = System.IO.Directory.GetCurrentDirectory();
            String executedFrom = System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            System.Console.WriteLine("Running executable from {0}", calledFrom);

            if (!calledFrom.Equals(executedFrom))
            {
                System.Console.WriteLine("Executable actually running in {0}", executedFrom);
                System.IO.Directory.SetCurrentDirectory(executedFrom);
            }

            XenClientGuestService service = new XenClientGuestService();
            service.OnServiceStart(args);

            System.Console.WriteLine("Press enter to end XenClientGuestService...");
            System.Console.ReadLine();

            service.OnServiceStop();
        }
#endif
        }