开发者

Why Doesn't This Loop Forever?

开发者 https://www.devze.com 2023-02-16 03:21 出处:网络
This code works great to clear the IECache but it seems like it should loop forever...Why doesn\'t it?

This code works great to clear the IECache but it seems like it should loop forever...Why doesn't it? Results is just outputtting what is and isn't deleted to a rich text box.

Thank you for your answers...

 public void delIECache(RichTextBox results)
        {
            results.Text += "Deleting IE Cache" + "\n";
            DirectoryInfo IECache = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
            MessageBox.Show(IECache.ToString());
            this.ClearFolder(IECache,results);
        }
        public void ClearFolder(DirectoryInfo diPath,RichTextBox results)
        {
            foreach (FileInfo fiCurrFile in diPath.GetFiles())
            {
                try
                {
                    fiCurrFile.Delete();
                    results.Text += "   " + fiCurrFile + " Successfully Deleted \n";

                }
                catch
                {
                    results.Text += "   " + fiCurrFile 开发者_如何学运维+ " Not Deleted \n";
                }
            }
            foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())
            {
                ClearFolder(diSubFolder,results);
            }


Because under normal circumstances you'll eventually hit folders that have no subfolders, and those branches of the recursion will end.

One way it might run forever (never tried this, but sounds possible in theory) is if you have cyclic folder with symbolic links.


diPath.GetFiles() is only executed once at the start of the loop, not every single iteration.


It's recursive, but eventually it will end when it gets to the end of each subdirectory. Wikipedia has a bit too much info on this, but there are some detailed explanations on recursive programming : http://en.wikipedia.org/wiki/Recursion_(computer_science)


I don't know C#, but the base case in your recursion occurs when there are no more sub directories. In that case, you do not call ClearFolder.


"foreach" isn't stream oriented. It looks only at what's currently in the collection. Do you need to keep this running "forever"?


It's recursively running through all the folders and their sub folders but unless there'd be a circular relation ship (which you can't have in your folder tree) you're going to get to the bottom of the folder tree at some point. That point is when the recursion stops and the method exits


It looks like correct loop. But maybe you have huge IE cache and it need long time (few minutes) to delete it file by file.


If this directory only contains a finite number of files, then it won't loop forever. The only way this could loop forever would be if at least one directory contains a hard link/junction which refers to itself or a directory which directly or indirectly contains this directory.

When all files and all subfolders are processed, then the method is done. The method calls itself if and only if the current directory has subdirectories.


Say I have the following directory tree:

IECache
|- IECache1
  |- IECache1a
|- IECache2
|- IECache3

These are all directories as the files here aren't important.

  1. Calls ClearFolder with a DirectoryInfo object for IECache
    1. Calls ClearFolder with a DirectoryInfo object for IECache1
      1. Calls ClearFolder with a DirectoryInfo object for IECache1a
      2. Returns to calling ClearFolder
    2. Calls ClearFolder with a DirectoryInfo object for IECache2
    3. Calls ClearFolder with a DirectoryInfo object for IECache3
    4. Returns to calling ClearFolder
  2. Returns to delIECache
0

精彩评论

暂无评论...
验证码 换一张
取 消