C++ C++/CLI:FileSystemWatcher移动或复制非空文件夹

C++ C++/CLI:FileSystemWatcher移动或复制非空文件夹,c++,events,c++-cli,filesystemwatcher,C++,Events,C++ Cli,Filesystemwatcher,我正在使用C++/CLI中的FileSystemWatcher。我在移动或复制非空文件夹时遇到问题:当将包含一个.txt文件的文件夹复制到监视文件夹时,会引发多个创建的和更改的事件,这很好,但当我移动同一文件夹时,只会引发文件夹的一个创建事件。问题是,我需要知道witch文件在其中,所以我的想法是在changed事件中创建一个循环,通过文件夹递归搜索。这适用于移动,但当我复制文件夹时,每个事件都会引发两次 我找不到算法,因此只引发一个文件夹和文件的create事件 谢谢你的帮助 代码: Syst

我正在使用C++/CLI中的FileSystemWatcher。我在移动或复制非空文件夹时遇到问题:当将包含一个.txt文件的文件夹复制到监视文件夹时,会引发多个
创建的
更改的
事件,这很好,但当我移动同一文件夹时,只会引发文件夹的一个创建事件。问题是,我需要知道witch文件在其中,所以我的想法是在
changed
事件中创建一个循环,通过文件夹递归搜索。这适用于移动,但当我复制文件夹时,每个事件都会引发两次

我找不到算法,因此只引发一个文件夹和文件的
create
事件

谢谢你的帮助

代码:

System::Void SnowDrive::Cloud::FileWatcher\u已更改(System::Object^sender,System::IO::FileSystemEventArgs^e)
{
文件大小;
字符串服务器节点,
FileName=c.marshal_as(e->Name),
FilePath=c.marshal_as(e->FullPath),
FtpPath=ToFtpPath(FilePath.substr(0,FilePath.find\u last\u of(“\\”));
if(FileName.find\u last\u of(“\\”)=string::npos)
FileName=FileName.substr(FileName.find\u最后一个\u(“\\”)+1);
for(unsigned int i=0;iToString()!=“”)
返回;
DIR*DIR;
dirent*FindData;
if((Dir=opendir(FilePath.c_str())==NULL)
返回;
while((FindData=readdir(Dir))!=NULL)
{
FileName=FindData->d_name;
如果(文件名==字符串(“.”| |文件名==字符串(“…”))
继续;
FileWatcher_已更改(gcnew字符串(“”),gcnew IO::FileSystemEventArgs(IO::WatcherChangeTypes::Created,gcnew字符串(FilePath.c_str()),gcnew字符串(FileName.c_str());
}
}
}
System::Void SnowDrive::Cloud::FileWatcher_已创建(System::Object^sender,System::IO::FileSystemEventArgs^e)
{
文件大小;
字符串FilePath=c.marshal_as(e->FullPath);
如果(!FileSystem::IsDir(FilePath))
{
FileSystem::FileSize(FilePath,&FileSize);
如果(文件大小!=0)
IgnoreFileList.push_back(FilePath);//忽略两次更改的事件
}
FileWatcher_已更改(新字符串(“”,e);
}

我不知道有什么方法可以让您只更改一个
事件。但是,我知道如何获取移动文件的事件。您需要附加到重命名的
事件

带文件的文件夹复制:文件夹的一个
已更改
事件,每个文件一个


文件夹随文件移动:文件夹有一个
已更改的
事件,每个文件有一个
重命名的
事件。

我测试了您所说的内容,但遗憾的是,当我随文件移动文件夹时,没有引发重命名事件是的,您是正确的。如果移动文件本身,则会收到文件移动的通知。如果移动包含文件的目录,则不会收到任何文件通知。
System::Void SnowDrive::Cloud::FileWatcher_Changed(System::Object^  sender, System::IO::FileSystemEventArgs^  e) 
{
    size_l  FileSize;

    string  ServerInode,
            FileName = c.marshal_as<std::string> (e -> Name),
            FilePath = c.marshal_as<std::string> (e -> FullPath),
            FtpPath  = ToFtpPath (FilePath.substr (0, FilePath.find_last_of ("\\")));

    if (FileName.find_last_of ("\\") != string::npos)
        FileName = FileName.substr (FileName.find_last_of ("\\") + 1);

    for (unsigned int i = 0; i < IgnoreFileList.size (); i++)
    {
        if (IgnoreFileList[i] == FilePath)
        {
            IgnoreFileList.erase (IgnoreFileList.begin () + i);

            return;
        }
    }

    if (!FileSystem::IsDir (FilePath))
    {
        FileSystem::FileSize (FilePath, &FileSize);

        if (FileSize != 0)
            IgnoreFileList.push_back (FilePath); // ignore twice changed events

        // do something
    }
    else
    {
        if (sender -> ToString () != " ")
            return;

        DIR * Dir;

        dirent * FindData;

        if((Dir = opendir(FilePath.c_str ())) == NULL)
            return;

        while ((FindData = readdir(Dir)) != NULL)
        {
            FileName = FindData -> d_name;

            if (FileName == string (".") || FileName == string (".."))
                continue;

            FileWatcher_Changed (gcnew String (" "), gcnew IO::FileSystemEventArgs (IO::WatcherChangeTypes::Created, gcnew String (FilePath.c_str ()), gcnew String (FileName.c_str ())));
        }
    }
}

System::Void SnowDrive::Cloud::FileWatcher_Created(System::Object^  sender, System::IO::FileSystemEventArgs^  e)
{
    size_l FileSize;

    string FilePath = c.marshal_as<std::string> (e -> FullPath);

    if (!FileSystem::IsDir (FilePath))
    {
        FileSystem::FileSize (FilePath, &FileSize);

        if (FileSize != 0)
            IgnoreFileList.push_back (FilePath); // ignore twice changed events
    }

    FileWatcher_Changed (gcnew String (" "), e);
}