繁体   English   中英

将C#控制台应用程序转换为服务(主要方法不起作用)

[英]Converting C# Console Application to a Service (Main method not working)

我最近“转换”或者更确切地将我的方法导入到默认的Windows服务模板中。 没有语法错误,它的编译没问题,但是FileSystemWatcher方法由于某种原因不起作用,例如,当正常运行时,它会将已创建的所有进程写入process.lst,但是当作为服务运行时它不会执行这(可能与工作目录有关,因为它是一个服务?):

namespace WindowsService
{
    class WindowsService : ServiceBase
    {
        /// <summary>
        /// Public Constructor for WindowsService.
        /// - Put all of your Initialization code here.
        /// </summary>
        public WindowsService()
        {
            this.ServiceName = "My Service";
            this.EventLog.Source = "My Service";
            this.EventLog.Log = "Application";

            // These Flags set whether or not to handle that specific
            //  type of event. Set to true if you need it, false otherwise.
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;

            if (!EventLog.SourceExists("My Service"))
                EventLog.CreateEventSource("My Service", "Application");
        }

        /// <summary>
        /// The Main Thread: This is where your Service is Run.
        /// </summary>
        static void Main()
        {
            ServiceBase.Run(new WindowsService());

            // This checks for any existing running instances, if found the proess is terminated immidieately.
            if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return;

            DisplayInfo();

            string dirPath = "C:\\";
            FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
            fileWatcher.IncludeSubdirectories = true;
            fileWatcher.Filter = "*.exe";
            // fileWatcher.Filter = "C:\\$Recycle.Bin";   
            //  fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);   
            fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
            //  fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);  
            //  fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);    
            fileWatcher.EnableRaisingEvents = true;
            // updated code

            while (true)
            {
                CleanUpDel();

                StartRemoveDuplicate();

                CompareFiles();

                bool changes = ScanFileChanges();

                if (!changes)
                {
                    TrimColon("process_trim.lst", "process_trimmed.lst");

                    TrimWipe();

                    AddTMPIgnore();

                    SendAlert();

                    CompareOrig();


                }
                Thread.Sleep(10000);
            }
        }


        private static void AddTMPIgnore()
        {
            var myString = File.ReadAllText("process_final.lst");
            File.AppendAllText("ignore_temp.lst", myString);
        }



        static void FileWatcher_Created(object sender, FileSystemEventArgs e)
        {

            using (StreamWriter fileWriter = new StreamWriter("process.lst", true))
            {
                var data = true;
                fileWriter.Write("C:\\" + e.Name + Environment.NewLine);
            }


        }

我已经完成了最后一次服务已经有一段时间了,所以我只记得模糊但是:

有一个OnStart和一个OnStop方法。 在此范围内,您必须创建一个完成工作的新线程。 您可以使用BackgroundWorker或创建System.Threading.Thread。 当我正确解释你的代码时,你在Main方法中进行处理。 这是不允许的。 该服务将无法正确初始化。 构造函数都不是这样做的地方。
还要确保,如果调用OnStop,您的处理逻辑就会停止。 否则,服务控制管理中心将不喜欢您的服务。

您的服务可能没有写入文件的权限,或者是将文件放在您不期望的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM