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

  • Home
  • SEARCH
  • 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 9128065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:25:32+00:00 2026-06-17T07:25:32+00:00

I have this code that is being repeated 3 times: private static void convert(object

  • 0

I have this code that is being repeated 3 times:

private static void convert(object source, FileSystemEventArgs f)
{
    string FileName;
    FileName = f.FullPath;

    string destinationFile = @"Y:\test\test.xml";
              System.Threading.Thread.Sleep(2000);
   try
   {

        Encoding utf8 = new UTF8Encoding(false);
        Encoding ansi = Encoding.GetEncoding(1256);
        System.Threading.Thread.Sleep(2000);

        string xml = File.ReadAllText(FileName, ansi);
       XDocument xmlDoc = XDocument.Parse(xml);
            **Console.WriteLine("1st");**
            File.WriteAllText(
               destinationFile,
                @"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
               utf8
            );
    }

Check the above in bold. It writes out 3 times. I have just put that to test. But why does it write out 3 times.. Meaning the file being written is also written 3 times.

I’m calling this function from a filesystemwatcher function to watch a folder if it has changed then take the file convert it to utf-8 and put it in the destination file.

EDIT 1:
Here is my watcher. Can you please check if this is fine:

private static void WatchFile()
    {
                watcher.Path = @"C:\project";

                   watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.xml";


        watcher.Changed += new FileSystemEventHandler(convert);
        watcher.Error += new ErrorEventHandler(WatcherError);
        Console.WriteLine("2nd");
        watcher.EnableRaisingEvents = true;


    }

Still don’t have a clue why it gets repeated 3 times.

EDIT 2:

Here goes’ my complete code:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;





class Test
{
 class Class1
{
    private static FileSystemWatcher watcher =
       new FileSystemWatcher();

    public static void Main()
    {
        WatchFile();
      Console.ReadLine();
     }

    private static void WatchFile()
    {
        watcher.Path = @"C:\project";

                    watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.xml";


        watcher.Changed += new FileSystemEventHandler(convert);
        watcher.Error += new ErrorEventHandler(WatcherError);
        Console.WriteLine("2nd");
        watcher.EnableRaisingEvents = true;

    }

    public static string CrL = "\r\n";

    private static void convert(object source, FileSystemEventArgs f)
    {
        string FileName;
        FileName = f.FullPath;

               string destinationFile = @"Y:\test\OnAirNow.xml";

                 System.Threading.Thread.Sleep(2000);
       try
       {

            Encoding utf8 = new UTF8Encoding(false);
            Encoding ansi = Encoding.GetEncoding(1256);
            System.Threading.Thread.Sleep(2000);

            string xml = File.ReadAllText(FileName, ansi);
           XDocument xmlDoc = XDocument.Parse(xml);
                Console.WriteLine("1st");
                File.WriteAllText(
                   destinationFile,
                    @"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
                   utf8
                );



        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }


    }

    private static void WatcherError(object source, ErrorEventArgs e)
    {

        Exception watchException = e.GetException();
                   watcher = new FileSystemWatcher();
        while (!watcher.EnableRaisingEvents)
        {
            try
            {
                                   WatchFile();
                Console.WriteLine("I'm Back!!");
            }
            catch
            {
                                    System.Threading.Thread.Sleep(2000);
            }
        }
    }


  }  
 }
  • 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-06-17T07:25:33+00:00Added an answer on June 17, 2026 at 7:25 am

    A common pattern using FileSystemWatcher is to set EnableRaisingEvents to false when starting processing the event:

    this.fileSystemWatcher = new FileSystemWatcher()
    {
        Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
        NotifyFilter = NotifyFilters.LastWrite,
        Filter = Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    };
    
    this.fileSystemWatcher.Changed += this.ConfigChanged;
    this.fileSystemWatcher.EnableRaisingEvents = true;
    

    and

    public void ConfigChanged(object sender, FileSystemEventArgs e)
    {
        try
        {
            this.fileSystemWatcher.EnableRaisingEvents = false;
            s_logger.Info("Configuration file changed.");
            // reload config here
            s_logger.Info("Configuration settings reloaded.");
        }
        catch (Exception exception)
        {
            s_logger.Error(exception.Message);
            s_logger.Error("Failed to reload configuration settings.");
        }
        finally
        {
            this.fileSystemWatcher.EnableRaisingEvents = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that runs but never stops. class A { public static
I currently have some code that is being repeated over and over, and it
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that reads from XML file. It gets five strings (groupId,
I have this code that suppose to place the marker by the latlng public
I have this code that where I would normally use one line: if (tableView
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);

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.