Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:46:02+00:00 2026-05-23T10:46:02+00:00

I have recently converted or rather imported my methods into a default Windows Service

  • 0

I have recently “converted” or rather imported my methods into a default Windows Service template. There are no syntax errors and its compiles ok, but the FileSystemWatcher method is not working for some reason, e.g. when run normally it will write all process that have been created to process.lst, but when running as a service it doesn’t do this (maybe something to do with the working directory, because its a service?):

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);
            }


        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:46:03+00:00Added an answer on May 23, 2026 at 10:46 am

    It’s quite a while I have done the last service, therefore I only remember vague but:

    There is a OnStart and a OnStop method. Within this, you have to create a new thread that does the job. You can use BackgroundWorker or create a System.Threading.Thread.
    When I interprete your code right, you do the processing in the Main-method. This is not allowed. The service will not proper initialize. Neither the constructor is the place for doing this.
    Make also sure, that if OnStop is called, your processing logic really stops. Otherwise Service Control Manager will not like your service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently converted 10 JavaScript files into one file, which I then run
I have recently converted my IPad application into a Universal Application. I am re-using
We have an existing (old) codeline that we have recently converted from VC6 to
I have a report, which was recently converted from SSRS2005 to SSRS2208. The report
We have a .NET application that was recently converted to .NET 4.0 and then
I have a setup for an application that was recently converted to be a
We have quite Large Web Based System, which is recently converted from Website to
I have a rather confusing issue. I've recently picked up some code I wrote
Hi I have datatype for a field recently converted to xml from varchar(MAX) in
We have recently converted an Access Application to store the date on SQL server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.