/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { bool mutexCreated = false; System.Threading.Mutex mutex = new System.Threading.Mutex(true, "EaseFilter", out mutexCreated); try { if (!mutexCreated) { Console.WriteLine("There are another EaseFilter service instance running, can't start the second one."); return; } Utils.CopyOSPlatformDependentFiles(); if (Environment.UserInteractive) { if (args.Length > 0) { string command = args[0]; switch (command.ToLower()) { case "-installdriver": { bool ret = FilterAPI.InstallDriver(); if (!ret) { Console.WriteLine("Install driver failed:" + FilterAPI.GetLastErrorMessage()); } else { Console.WriteLine("Install driver succeeded."); } break; } case "-uninstalldriver": { FilterAPI.StopFilter(); bool ret = FilterAPI.UnInstallDriver(); if (!ret) { Console.WriteLine("UnInstall driver failed:" + FilterAPI.GetLastErrorMessage()); } else { Console.WriteLine("UnInstall driver succeeded."); } break; } case "-installservice": { try { ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); } catch (Exception ex) { Console.WriteLine("Install service failed:" + ex.Message); } break; } case "-uninstallservice": { try { ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); } catch (Exception ex) { Console.WriteLine("UnInstall service failed:" + ex.Message); } break; } case "-console": { try { Console.WriteLine("Starting FileProtectorCS console application..."); GlobalConfig.EventLevel = EventLevel.Trace; GlobalConfig.EventOutputType = EventOutputType.Console; FilterWorker.StartService(); Console.WriteLine("\n\nPress any key to stop program"); Console.Read(); FilterWorker.StopService(); } catch (Exception ex) { Console.WriteLine("Start FileProtectorCS service failed:" + ex.Message); } break; } case "-enable": { try { Console.WriteLine("Enable FileProtectorCS service..."); GlobalConfig.EventLevel = EventLevel.Trace; GlobalConfig.EventOutputType = EventOutputType.Console; FilterAPI.UnInstallDriver(); bool ret = FilterAPI.InstallDriver(); if (!ret) { Console.WriteLine("Install driver failed:" + FilterAPI.GetLastErrorMessage()); } else { Console.WriteLine("Install driver succeeded."); } FilterWorker.StartService(); FilterWorker.StopService(); Console.WriteLine("Enable FileProtectorCS completed."); } catch (Exception ex) { Console.WriteLine("Start FileProtectorCS service failed:" + ex.Message); } break; } case "-disable": { try { FilterAPI.ResetConfigData(); Console.WriteLine("Disable FileProtectorCS completed."); } catch (Exception ex) { Console.WriteLine("Disable FileProtectorCS service failed:" + ex.Message); } break; } default: Console.WriteLine("The command " + command + " doesn't exist"); PrintUsage(); break; } } else { PrintUsage(); } } else { Console.WriteLine("Starting EaseFilter windows service..."); EaseFilterService service = new EaseFilterService(); ServiceBase.Run(service); } } catch (Exception ex) { EventManager.WriteMessage(132, "EaseFilterService", EventLevel.Error, "EaseFilter failed with error " + ex.Message); } finally { Console.WriteLine("Exiting EaseFilter service."); GlobalConfig.Stop(); mutex.Close(); } }
protected override void OnStart(string[] args) { FilterWorker.StartService(); }