0

我在 C# 中编写了一个监视文件夹的代码,当文件被创建时,代码会执行一些操作并将结果写入日志文件。我有这种非常奇怪的行为。当 i 文件在监视文件夹中创建时,处理更改的函数将执行两次!即使它只是一个更改事件。最初我使用 FileSystemWatcher。但我查了一下之后,我发现它有很多稳定性,所以我切换到 MyFileSystemWatcher,这是一个更稳定的暗示。但我的日志文件中仍然有重复项。我不知道为什么用于查找更改的更改代码会同时运行。这是代码示例

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

这是 onChange 函数

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

任何帮助都会非常感激!

4

1 回答 1

0

处理了 onChange 函数。添加时间和文件名来处理重复点击

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

                    }
                }
            }
        }
    }
于 2013-10-23T08:56:02.700 回答