[VB.net-WinAPP] - Slow Internet Cache

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zubair1
    New Member
    • Sep 2008
    • 79

    [VB.net-WinAPP] - Slow Internet Cache

    Hi,

    Code:
        Public Sub FindInternetCache()
            Dim cacheDir As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
    
            Dim cacheFiles As ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(cacheDir, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
    
            Dim fCounter As Integer = 0
            Dim fSize As Double
    
    
            For Each cacheFound As String In cacheFiles
                fCounter = fCounter + 1
    
    
            DisplayBox.Text &= Environment.NewLine
            DisplayBox.Text &= "Found: " & cacheFound & Environment.NewLine
    
    
                fSize += My.Computer.FileSystem.GetFileInfo(cacheFound).Length
            Next
    
    
            DisplayBox.Text &= Environment.NewLine
            DisplayBox.Text &= "IE Temporary Internet Files Found " & "(" & fCounter & " Files) " & SetBytes(fSize) & Environment.NewLine
    
    
        End Sub
    I am using the above function to Output all the files in the Temporary Internet Files folder - its working alright. But its very slow and also hangs.

    I was wondering if theres a better method to do this.

    Thank you
    Regards
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Is it faster if you use the the .NET Directory.GetFi les() function and not that strange Computer.FileSy stem call?

    Comment

    • zubair1
      New Member
      • Sep 2008
      • 79

      #3
      Originally posted by Plater
      Is it faster if you use the the .NET Directory.GetFi les() function and not that strange Computer.FileSy stem call?
      it seems to be the same

      i used it like this to find all files

      Code:
      Directory.GetFiles(cacheDir, "*.*", SearchOption.AllDirectories)
      :(

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Try using the DirectoryInfo object. See if that is any faster.

        Comment

        • zubair1
          New Member
          • Sep 2008
          • 79

          #5
          Originally posted by insertAlias
          Try using the DirectoryInfo object. See if that is any faster.
          Oops, sorry i forgot about this :(

          I tried the directoryInfo object with no help.

          :(

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well the temporary internet cache is usually PACKED with files... I have over 3k in mine right now.
            It takes IE quiet a long time to flush the cache as well.

            Also, why are you doing this??
            fCounter = fCounter + 1

            Why not just say fCounter=cacheF iles.Length after the loop (or before it, it does not matter

            Comment

            • zubair1
              New Member
              • Sep 2008
              • 79

              #7
              Originally posted by Plater
              Well the temporary internet cache is usually PACKED with files... I have over 3k in mine right now.
              It takes IE quiet a long time to flush the cache as well.

              Also, why are you doing this??
              fCounter = fCounter + 1

              Why not just say fCounter=cacheF iles.Length after the loop (or before it, it does not matter
              Hi,

              it has alot of files but i've seen many apps made in VB and C# that shows all the cache files to the end user but works perfectly fine :(

              yeah, i learned that very late about using .length =P

              but i still use fCounter to determine how many files were deleted.
              i also removed that thinking maybe it could be slowing down :( but that didn't help either :(

              :(

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Well I just implented it and I can tell you that its the string concatination that is killing you. I re-wrote it to use a StringBuilder object instead of just concatinating text to the texbox.
                Here is the last line (I am not pasting in the Found: lines since there is alot):
                IE Temporary Internet Files Found (10492 Files) 203080648

                It took .8 seconds. Now granted that time was recorded after the whole thing had been searched durring testing, so it was all indexed for faster searching, but it wasn't too much longer.

                So consider making your function return a string value (from a stringbuilder) rather then working directly with a textbox.

                Comment

                • zubair1
                  New Member
                  • Sep 2008
                  • 79

                  #9
                  Originally posted by Plater
                  Well I just implented it and I can tell you that its the string concatination that is killing you. I re-wrote it to use a StringBuilder object instead of just concatinating text to the texbox.
                  Here is the last line (I am not pasting in the Found: lines since there is alot):
                  IE Temporary Internet Files Found (10492 Files) 203080648

                  It took .8 seconds. Now granted that time was recorded after the whole thing had been searched durring testing, so it was all indexed for faster searching, but it wasn't too much longer.

                  So consider making your function return a string value (from a stringbuilder) rather then working directly with a textbox.
                  Hi,

                  WoW - i was having a doubt that it might be something with the way of outputing the results but i'm still not sure how you did it :(

                  Is it possible for you to paste the code please =(

                  Would really appreciate that

                  Regards

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Well you are aware that string concatination is a degrading effect right? (I only recenlty learned just how BAD it gets)
                    Everytime you do a &= (or += in c#), it recreates the entire string. So as your string gets longer, it takes more and more time. With a StringBuilder, it does not re-create the string, it does what you would think the &= does, just append things to the end of the string.

                    I wrote my code in C#, but you could probably see what the changes where:
                    [code=c#]
                    private string FindInternetCac he()
                    {
                    DateTime Start = DateTime.Now;//Only used for timing
                    StringBuilder retval = new StringBuilder(" ");
                    int fCounter = 0;
                    double fSize = 0.0;
                    string cacheDir = "";

                    cacheDir= Environment.Get FolderPath(Envi ronment.Special Folder.Internet Cache);
                    DirectoryInfo di = new DirectoryInfo(c acheDir);
                    FileInfo[] cacheFiles=di.G etFiles("*.*", SearchOption.Al lDirectories);

                    foreach (FileInfo cacheFound in cacheFiles)
                    {
                    fCounter = fCounter + 1;
                    fSize += cacheFound.Leng th;
                    retval.AppendFo rmat("Found: {0}\r\n", cacheFound.Full Name);
                    }
                    retval.AppendFo rmat("\r\nIE Temporary Internet Files Found ({0} Files) {1}\r\n", fCounter, fSize);
                    DateTime End = DateTime.Now;//Only used for timing
                    TimeSpan t = (End - Start);//Only used for timing

                    return retval.ToString ();
                    }
                    [/code]

                    Comment

                    • zubair1
                      New Member
                      • Sep 2008
                      • 79

                      #11
                      Awesome,

                      Thanks for the code Platter :) looks great

                      and also thanks alot for letting me know about &= never knew it could be that bad :(

                      I tried using this method - the StringBuilder method but it isn't working :(

                      i get declare errors when trying to use StringBuilder command

                      Code:
                      StringBuilder retval = new StringBuilder("");
                      is the stringbuilder on for C#

                      how can i use this on VB.net (vb2005)

                      Thanks in advance

                      Comment

                      • Curtis Rutland
                        Recognized Expert Specialist
                        • Apr 2008
                        • 3264

                        #12
                        The StringBuilder is in the System.Text namespace.

                        So, you could use
                        Code:
                        Imports System.Text
                        or you can declare it as
                        Code:
                        Dim retval As New System.Text.StringBuilder("")

                        Comment

                        • zubair1
                          New Member
                          • Sep 2008
                          • 79

                          #13
                          Originally posted by insertAlias
                          The StringBuilder is in the System.Text namespace.

                          So, you could use
                          Code:
                          Imports System.Text
                          or you can declare it as
                          Code:
                          Dim retval As New System.Text.StringBuilder("")
                          Too Good =D

                          Thanks it worked that way :)

                          Comment

                          • zubair1
                            New Member
                            • Sep 2008
                            • 79

                            #14
                            THANK YOU PLATTER :)

                            it worked :)

                            The method you showed worked :)

                            its working pretty fast now :)

                            Thanks for the marvelous help :) you're the best ;)

                            Comment

                            Working...