简体   繁体   English

如何检查文件夹的内容是否已更改

[英]How can I check if the content of a folder was changed

I need a procedure that checks if new folders/files were added to a given selected folder. 我需要一个检查新文件夹/文件是否添加到给定选定文件夹的过程。 I need this procedure to run upon application start up so the processing time at this stage is important. 我需要此过程在应用程序启动时运行,因此此阶段的处理时间很重要。

I guess I can make a log of current state, log of the previous state, sort and compare them. 我想我可以记录当前状态,记录先前状态,对其进行排序和比较。

  1. First I need to know if there is another way. 首先,我需要知道是否还有另一种方法。

  2. Second if there is no other way what is the best way to find difference between two lists of files paths: both structure and algorithms. 其次,如果没有其他方法,什么是找到两个文件路径列表之间的差异的最佳方法:结构和算法。

Old state: 旧状态:

c:\firstfolder\a.doc
c:\firstfolder\b.doc
c:\firstfolder\secondFolder\a.doc
c:\firstfolder\secondFolder\b.doc

New state: 新状态:

c:\firstfolder\a.doc
c:\firstfolder\b.doc 
c:\firstfolder\secondFolder\a.doc 
c:\firstfolder\secondFolder\b.doc 
c:\firstfolder\secondFolder\c.doc

I'm looking for c:\\firstfolder\\secondFolder\\c.doc . 我在寻找c:\\firstfolder\\secondFolder\\c.doc

您可以使用FileSystemWatcher类,我认为它确实在做您所追求的。

I think that the Sync Framework may be what you are looking for. 我认为Sync Framework可能就是您想要的。

http://msdn.microsoft.com/en-us/sync/default.aspx http://msdn.microsoft.com/zh-CN/sync/default.aspx

Since your application is not running continuously, using a FileSystemWatcher does not seem like a good option for you, but I did want to mention it since it might spark some ideas on your side. 由于您的应用程序无法连续运行,因此使用FileSystemWatcher似乎不是一个不错的选择,但我确实想提及它,因为它可能会激发您一些想法。

If you have a list of the old state and create a list of the new state you can use an algorithm like the one outlined here to compare the two lists to determine additions and deletions since the first list was created. 如果您有一个旧状态列表并创建一个新状态列表,则可以使用此处概述的算法来比较两个列表,以确定自创建第一个列表以来的添加和删除操作。 That accepted answer to the provided link also has a nice solution that works very well if the lists are not sorted. 提供的链接的公认答案也有一个不错的解决方案,如果未对列表进行排序,则效果很好。

I think the best bet is recursing through the directory and saving the state. 我认为最好的选择是遍历目录并保存状态。 Something like this: 像这样:

public List<FileData> RecurseDirectory(string directory, int level, out int files, out int folders)
        {
            IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
            //long size = 0;
            files = 0;
            folders = 0;
            WIN32_FIND_DATA findData;
            List<FileData> list = new List<FileData>();

            IntPtr findHandle;

            // add global escape chars if not present.
            if (directory.StartsWith(@"\\?\"))
            {
                findHandle = FindFirstFile(directory + @"\*", out findData);
            }
            else
            {
                findHandle = FindFirstFile(@"\\?\" + directory + @"\*", out findData);
            }


            if (findHandle != INVALID_HANDLE_VALUE)
            {
                //if file exists:


                do
                {
                    if ((findData.dwFileAttributes & FileAttributes.Directory) != 0)
                    {
                        //if it's a directory:

                        if (findData.cFileName != "." && findData.cFileName != "..")
                        {
                            //increment folder counter.
                            folders++;

                            int subfiles, subfolders;
                            string subdirectory = directory + (directory.EndsWith(@"\") ? "" : @"\") +
                                findData.cFileName;
                            //Console.WriteLine("Dir:" + subdirectory);

                            //add it to list
                            String path = subdirectory;
                            FileData tempFileData = new FileData();
                            tempFileData.path = path;
                            tempFileData.fileProperties = findData;
                            list.Add(tempFileData);

                            //allows -1 to do complete search.
                            if (level != 0)
                            {
                                //recurse through to next subfolder
                                list.AddRange(RecurseDirectory(subdirectory, level - 1, out subfiles, out subfolders));

                                folders += subfolders;
                                files += subfiles;
                            }
                        }
                    }
                    else
                    {
                        // File
                        files++;
                        string subfile = directory + (directory.EndsWith(@"\") ? "" : @"\") +
                                findData.cFileName;
                        //Console.WriteLine("File:" + subfile);
                        //list.Add("File:" + subfile);
                        String path = subfile;
                        FileData t = new FileData();
                        t.path = path;
                        t.fileProperties = findData;
                        list.Add(t);

                        //size += (long)findData.nFileSizeLow + (long)findData.nFileSizeHigh * 4294967296L;
                    }
                }
                while (FindNextFile(findHandle, out findData));
                FindClose(findHandle);

            }

            return list;
        }

After this, you'll have the list of the state. 之后,您将获得状态列表。 If you have the old-state in the list also. 如果列表中也有旧状态。 Compare the lists Now. 现在比较列表。

You can save time info of the last state. 您可以保存最后状态的时间信息。 Then you just can compare the creation times of files and folders with that. 然后,您可以将其与文件和文件夹的创建时间进行比较。 The following code may give you an idea: 以下代码可能会给您一个想法:

foreach(FileInfo f in dir.GetFiles() {
   if(f.CreationTime > dtLastState ) {
         //do some interesting stuff
   }
} 

Edit: This is not a complete answer. 编辑:这不是一个完整的答案。 You cannot get the deleted files by this code. 您无法通过此代码获取已删除的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM