Search code examples
c#filesystemwatcher

Folder Watch when File is Created


im writing a code in C# that watches a folder and when a file gets created the code makes some actions and writes the results to a log file. im having this very strange behaviour. when i file gets created in the watched folder the function that handles the change is executed twise! even if it is only one change event. initially i used FileSystemWatcher. but i after looking it up, i saw that it has meny stability issued so i switched to MyFileSystemWatcher which is a much more stable impliminatation. but im still getting duplications in my log file. i have no idea why the code that is in chanrge for looking up the change runs twise. here is the code sample

protected void Folder_Watch(string path)
    {
        if (!Directory.Exists(path))
        {
            try
            {
                System.IO.Directory.CreateDirectory(path);
            }
            catch (Exception ex)
            {
                File.AppendAllText(logPath + "\\SSHErrorLog.log", "[]*******" + DateTime.Now + " Error: " + ex.Message.ToString() + Environment.NewLine);
            }
        }
        MyFileSystemWatcher m_Watcher = new MyFileSystemWatcher(path);
        //m_Watcher.Path = path;
        m_Watcher.Filter = "*.*";
        m_Watcher.NotifyFilter = NotifyFilters.FileName;
        m_Watcher.Created += new FileSystemEventHandler(OnChanged);
        m_Watcher.EnableRaisingEvents = true;
    }

here is the onChange function

 private void OnChanged(object source, FileSystemEventArgs e)
    {
        File.AppendAllText(logPath + "\\SSHConnectionLog.log", "[]*******" + DateTime.Now + " OnChanged function: " + Environment.NewLine);
        // Decrypt the file.
        DecryptFile(keyPath + "\\id_rsa_Encrypted", keyPath + "\\id_rsa", sSecretKey);
        // Remove the Key from memory. 
        //PKey = new PrivateKeyFile(keyPath + "\\id_rsa");
        keyResult.Text = "RSA keys Were Generated at:" + keyPath;
        //ScpClient client = new ScpClient("remnux", "adi", PKey);
        Chilkat.SFtp client = new Chilkat.SFtp();
        string[] tempPath = e.FullPath.Split('\\');
        string fullPathNew = string.Empty;
        for (int i = 0; i < tempPath.Length - 1; i++)
        {
            fullPathNew += tempPath[i];
        }
        if (Directory.Exists(fullPathNew))
        {
            sshConnect(client);
            File_Upload(e.FullPath, client);
        }
        else
        {
            try
            {
                sshConnect(client);
                System.IO.Directory.CreateDirectory(fullPathNew);
                File_Upload(e.FullPath, client);
            }
            catch (Exception ex)
            {
                File.AppendAllText(logPath + "\\SSHErrorLog.log", "[]*******" +                        DateTime.Now + " Error in OnChanged function: " + ex.Message.ToString() +    Environment.NewLine);
            }
        }
    }

any help would be very much appriciated!


Solution

  • handled the onChange function. added time and file name to handle duplicate hits

    private void OnChanged(object source, FileSystemEventArgs e)
        {
            string[] temp = new string[3];
            string[] tempNow = new string[3];
            string[] tempSeconds = new string[2];
            string[] tempNowSeconds = new string[2];
            int temp1 = 0;
            int temp2 = 0;
            if(string.IsNullOrEmpty(changeName))
            {
                changeName = e.Name;
            }
            if (string.IsNullOrEmpty(changeTime))
            {
                changeTime = DateTime.Now.ToString();
                temp = this.changeTime.Split(':');
                tempNow = DateTime.Now.ToString().Split(':');
                tempSeconds = temp[2].Split(' ');
                tempNowSeconds = temp[2].Split(' ');
                temp1 = Convert.ToInt16(tempSeconds[0]);
                temp2 = Convert.ToInt16(tempNowSeconds[0]);
                // Decrypt the file.
                DecryptFile(keyPath + "\\id_rsa_Encrypted", keyPath + "\\id_rsa", sSecretKey);
                // Remove the Key from memory. 
                PKey = new PrivateKeyFile(keyPath + "\\id_rsa");
                keyResult.Text = "RSA keys Were Generated at:" + keyPath;
                ScpClient client = new ScpClient("remnux", "adi", PKey);
                string[] tempPath = e.FullPath.Split('\\');
                string fullPathNew = string.Empty;
                for (int i = 0; i < tempPath.Length - 1; i++)
                {
                    fullPathNew += tempPath[i];
                }
    
                if (Directory.Exists(fullPathNew))
                {
                    sshConnect(client);
                    File_Upload(e.FullPath, client);
                }
                else
                {
                    try
                    {
                        sshConnect(client);
                        System.IO.Directory.CreateDirectory(fullPathNew);
                        File_Upload(e.FullPath, client);
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(logPath + "\\SSHErrorLog.log", "[]*******" + DateTime.Now + " Error in OnChanged function: " + ex.Message.ToString() + Environment.NewLine);
                    }
    
                }
            }
            if (!this.changeTime.Equals(DateTime.Now.ToString()))
            {
                temp = this.changeTime.Split(':');
                tempNow = DateTime.Now.ToString().Split(':');
                tempSeconds = temp[2].Split(' ');
                tempNowSeconds = temp[2].Split(' ');
                temp1 = Convert.ToInt16(tempSeconds[0]);
                temp2 = Convert.ToInt16(tempNowSeconds[0]);
                if (temp[2] != tempNow[2])
                {
                    if ((temp1 < temp2 + 10 || temp1 > temp2 +40) && e.Name != changeName)
                    {
                        // Decrypt the file.
                        DecryptFile(keyPath + "\\id_rsa_Encrypted", keyPath + "\\id_rsa", sSecretKey);
                        // Remove the Key from memory. 
                        PKey = new PrivateKeyFile(keyPath + "\\id_rsa");
                        keyResult.Text = "RSA keys Were Generated at:" + keyPath;
                        ScpClient client = new ScpClient("remnux", "adi", PKey);
                        string[] tempPath = e.FullPath.Split('\\');
                        string fullPathNew = string.Empty;
                        for (int i = 0; i < tempPath.Length - 1; i++)
                        {
                            fullPathNew += tempPath[i];
                        }
    
                        if (Directory.Exists(fullPathNew))
                        {
                            sshConnect(client);
                            File_Upload(e.FullPath, client);
                        }
                        else
                        {
                            try
                            {
                                sshConnect(client);
                                System.IO.Directory.CreateDirectory(fullPathNew);
                                File_Upload(e.FullPath, client);
                            }
                            catch (Exception ex)
                            {
                                File.AppendAllText(logPath + "\\SSHErrorLog.log", "[]*******" + DateTime.Now + " Error in OnChanged function(second if): " + ex.Message.ToString() + Environment.NewLine);
                            }
    
                        }
                    }
                }
            }
        }