# VBForums CodeBank > CodeBank - Visual Basic .NET >  Temp File Cleaner

## weirddemon

Every now and then, I'll see people ask how to get the temp files for some location. It's usually places like IE's directories, or the system temp, etc. In one of my other projects, I use CCleaner to clear up temp files prior to other automated tasks and I wanted to decrease my dependency on 3rd party software. So I decided to duplicate the features of CCleaner in my own app, I'm temporarily calling it ACleaner. I couldn't think of a name, so that's what I got :P



I decided that I wanted to share this with everyone, so if anyone wanted to do something similar or needed to know a temp path, etc, this would help them. But, before I continue, please be aware of two things.

1. You can use my work in any way, just don't take credit for it.
2. It's not quite complete. It's mostly done because all of the "hard" work is done, but there are still some small things to finish up.

With that, let's move on to the code!

When I first started making this, I tried to keep ease of use in mind. I wanted anyone who wanted to use it, would be able to with just a few lines of code. So, if anyone has any better ideas on how I'm doing this, please let me know.

There are 4 main classes to be aware of.

*FileInfoA*, *DirectoryInfoA*, *RegistryInfoA*, and *UniducksCleaner*.

The last class is what actually does all of the work and the other three are just classes to pass information to the worker class. For the moment, you can't scan any registry keys, but I should finish that up soon. So no worries there.

With the worker class, since I'm working with a ListView, I hard coded the ListView into the class, which isn't ideal, but was the easiest option at the time. I'll probably go back later and remove that so it just returns a list that can be manipulated.

Before you can start scanning, you need to create a new instance of the the "Info" classes and pass them to the new instance of the worker class you created. Again, the registry isn't working yet, so I'll show an example for the other classes.


VB.NET Code:
Dim cleaner As New UniducksCleaner 'Worker Class
 Dim di As New DirectoryInfoA(InternetExplorerCookies, "Internet Explorer - Cookies", 3, "*.*", True)
Dim fi As New FileInfoA(GoogleChromeCookies, "Google Chrome - Cookies", 1, "*.*")
Dim ri As New RegistryInfoA("hkcu", VisualStudio2010FileMRUList, False, "Visual Studio - VS 2010 File MRU List", 13)
 cleaner.Add(di)
clean.Add(fi)
cleaner.Add(ri)

The *Add* method only accepts a FileInfoA, DirectoryInfoA or a RegistryInfoA class. You can't pass a normal string to it becuase the other classes force you pass additional information the worker class needs to execute properly.

For the DirectoryInfoA class, there are 5 arguments, but they're pretty simple. I'll list them in order.

1. Path to file or directory. You'll notice I just passed a premade string here. All of the strings I've compiled are in the "Paths" module.
2. This is just a description for the ListView
3. ImageIndex for the ListView
4. Search filter.
5. If True, the worker class will recurse through the directory.

The FileInfoA class has just the first 4 and the RegistryInfoA class has just two.

After you've added the info classes, just call *Scan*.

All of the paths are kept in a module called, "Paths", so if you just need to know where a particular temp location is, just check there.

I've probably missed some information, so just check out the sample project and play around with it. Please feel free to ask any questions.

**UPDATE**
I changed the scanning methods to make them a lot more accurate.
I also added more directories.
Added some UI features and changed other UI features up a little bit.

**UPDATE 2**
Fixed a few small bugs
Finished adding all current directories and files

**UPDATE 3**
Added basic registry scanning - Current registry MRUs = VS 2010 File/Project MRU Lists

----------


## formlesstree4

Really nice little insert here weirddemon; one good reason to be a programmer is exactly what you said: "decrease our dependence on 3rd party software"  :Smilie: 

EDIT: I haven't downloaded yet, but you hardcoded the ListView in...maybe you could make an option to pass a reference to a ListView and manipulate it that way? That way it's a really simple class that could take the ListView when initializing it:


```
Dim _x As New UniducksCleaner(ListView1)
```

----------


## weirddemon

> Really nice little insert here weirddemon; one good reason to be a programmer is exactly what you said: "decrease our dependence on 3rd party software" 
> 
> EDIT: I haven't downloaded yet, but you hardcoded the ListView in...maybe you could make an option to pass a reference to a ListView and manipulate it that way? That way it's a really simple class that could take the ListView when initializing it:
> 
> 
> ```
> Dim _x As New UniducksCleaner(ListView1)
> ```


Thanks, formlesstree4.

I suppose I could put the ListView in the constructor, like you're mentioning. But it still requires the user to pass a ListView to the class. At the moment, the ListView is passed to the .Start method of the UniducksClass. Like so:


VB.NET Code:
Private clean As New UniducksCleaner Dim fdi As New FileDirectoryInfo("dir", InternetExplorerCookies, "Internet Explorer - Cookies", 3, "*.*", True)clean.Add(fdi) clean.Scan(lvwTemps)

I was thinking of just using a function and returning a List that could be manipulated. But that brings about it's own problems. Having the ListView in there makes everything much easier.

----------


## formlesstree4

> Thanks, formlesstree4.
> 
> I suppose I could put the ListView in the constructor, like you're mentioning. But it still requires the user to pass a ListView to the class. At the moment, the ListView is passed to the .Start method of the UniducksClass. Like so:
> 
> 
> VB.NET Code:
> Private clean As New UniducksCleaner
 Dim fdi As New FileDirectoryInfo("dir", InternetExplorerCookies, "Internet Explorer - Cookies", 3, "*.*", True)
clean.Add(fdi)
 clean.Scan(lvwTemps)


Ahh, I did not know that! Well, still, great little piece of code here!

----------


## moti barski

can it backup the default browser favorite list ?
like in maxthone 2 browser there was an import export button to save favorite (url list)
to and from a folder.

other than that, I like the UI.

----------


## weirddemon

> can it backup the default browser favorite list
> like in maxthone 2 browser there was an import export button to save favorite (url list)
> to and from a folder.
> 
> other than that, I like the UI.


Not inherently. But that's incredibly simple. I can't imagine it would take more than a few minutes to implement such a feature.

I might consider it when I'm done with the other things.

Thanks

----------


## weirddemon

I've updated ACleaner to include more directories while also fixing the scanning method.

I thought the scanning method was working just fine because everything was matching up correctly during my testing. I would run my app with specific directories and then manually look up that directory. I'd compare number of files and directory size.

But, when I started scanning the "User Temp Files" directory, it made 300 MBs of data turn into 386 GB! That was crazy off. So now it looks like it's working out better now. But please let me know if you guys run into any offsets.

I also changed the icon. Like I mentioned before, I'm going to use this app in another project, so I changed the icon to reflect that.

I also divided up the "FileDirectoryInfo" class into two classes. Previously, this class was used for both files and directories, but I split it up to alleviate confusion and reduce the amount of arguments needed when creating these classes. Now there's three classes used to pass to the main class: *FileInfoA*, *DirectoryInfoA* and *RegistryInfo*.

Again, clicking "Clean" doesn't really do anything at the moment. I'm waiting to finish all other directories before I work on it. But, if you really want to use it, it's not hard to get it working.

When the "Scan" method is called, it adds each file scanned to a property called "FilesToDelete." So all you have to do is loop through that list and delete each one.

Please try out the app and tell me what you guys think and if there's any improvements I should make or application temp files I should add.

----------


## nbrege

As a fan of CCleaner I wanted to give your program a try, but ran into some errors.  It seems to be derailing when it encounters inaccessible directories.  I got this error as soon as I ran the program:

"Access to the path 'C:\Windows\CSC\v2.0.6' is denied."

This was in the MasterDirectoryScanner function.

----------


## weirddemon

> As a fan of CCleaner I wanted to give your program a try, but ran into some errors.  It seems to be derailing when it encounters inaccessible directories.  I got this error as soon as I ran the program:
> 
> "Access to the path 'C:\Windows\CSC\v2.0.6' is denied."
> 
> This was in the MasterDirectoryScanner function.


That's not entirely true, now is it?  :Wink: 

I'm willing to bet that the only directory you're having problems with is this one directory. Which, if I remember correctly was only an issue with "Windows Log Files" and "Windows Error Reporting" directories.

ACleaner runs as administrator. Which means it has access to every directory you have access to. So this tells us that you don't actually have access to this directory. If you try opening this directory on your own, manually, you'll get the following security message. 


If you click "Continue", you'll then get this message:


So then you have to open the security tab and manually give yourself access. Even if your account is an administrator, you still have to complete this process.

I'm not suggesting you do this though, I'm only bringing it up to illustrate the point.

I haven't begun looking into a fix though. I'll do that when I'm done adding all the temp locations to the UI.

----------


## nbrege

Well I'm not saying you're wrong ... just pointing out that your program doesn't handle inaccessible folders gracefully.   :Wink:

----------


## weirddemon

> Well I'm not saying you're wrong ... just pointing out that your program doesn't handle inaccessible folders gracefully.


You're not understanding the point. My application handles inaccessible directories and files just fine. This is an _exception_ and nothing more. The solution isn't to just skip it. The solution is to figure out how to access it.

----------


## nbrege

OK, whatever you choose to call it ... just pointing it out to help you debug.  I also got an error when all checkboxes were unchecked & I clicked Scan.  Don't remember the error now as I don't have the program open right now...

----------


## weirddemon

> OK, whatever you choose to call it ... just pointing it out to help you debug.  I also got an error when all checkboxes were unchecked & I clicked Scan.  Don't remember the error now as I don't have the program open right now...


I'm calling it what it is. How I'm handling inaccessible items is fine. I've gone through and ensured that what I'm not able to access is passed along. I can't just skip this directory though. That would be sloppy at best.

I've proven that this is not a normal situation and it will take some research for me to figure out how to access it.

The error you received basically explained that all the CheckBoxeswere unchecked. I didn't put any catches in their yet for that. But... I don't understand why'd you click scan without checking one anyway.

----------


## formlesstree4

> I'm calling it what it is. How I'm handling inaccessible items is fine. I've gone through and ensured that what I'm not able to access is passed along. I can't just skip this directory though. That would be sloppy at best.
> 
> I've proven that this is not a normal situation and it will take some research for me to figure out how to access it.
> 
> The error you received basically explained that all the CheckBoxeswere unchecked. I didn't put any catches in their yet for that. But... I don't understand why'd you click scan without checking one anyway.


Because? Just to see if the program would do anything?

----------


## weirddemon

> Because? Just to see if the program would do anything?


That's fine, but that's not really an unforeseen error that needs reported or explaining.

----------


## formlesstree4

> That's fine, but that's not really an unforeseen error that needs reported or explaining.


Well no but it can make for some funny moments in a program :P

----------


## weirddemon

I've updated ACleaner once more. I fixed a couple of small bugs and I've finished adding all the directories/files. Now I'm going to work on scanning the registry.

----------


## weirddemon

Another update.

I've added the ability to scan the registry. It's not completely done, but it will do basic scanning of a top level key and pull its values.

I've added VS 2010's MRU lists and I'm going to add VS 2005, 2008 and finish IE's MRU items.

----------


## wingers

Hi

Great project and very useful, but I get this error when launching it from ouside of VB, any ideas?

Microsoft .NET Framework - unhandled exception - arithmetic operation resulted in an overflow

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.OverflowException: Arithmetic operation resulted in an overflow.
   at ACleaner.UniducksInformation.GetComputerInformation()
   at ACleaner.frmMain.frmMain_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5446 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
ACleaner
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Darren%20Rose/Desktop/ACleaner.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5442 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5446 (Win7SP1GDR.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Core
    Assembly Version: 3.5.0.0
    Win32 Version: 3.5.30729.5420 built by: Win7SP1
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Accessibility
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Management
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Management/2.0.0.0__b03f5f7f11d50a3a/System.Management.dll

----------


## wingers

P.S. It appears to be coming from this line of code


vb Code:
Return getOS() & ", " & getProcessor() & ", " & returnSize(CInt(My.Computer.Info.TotalPhysicalMemory)) & ", " & GetGraphicsCardName()

I also get the following 4 warnings in VB:-

Warning	1	Variable 'osinfo' is passed by reference before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use	C:\Users\Programming\Desktop\temp cleaner source\ACleaner\Classes\UniducksInformation.vb	86	34	ACleaner

Warning	2	Function 'mozillaFireFoxPaths' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.	C:\Users\Programming\Desktop\temp cleaner source\ACleaner\Modules\Paths.vb	128	5	ACleaner

Warning	3	WithEvents variable 'size' conflicts with property 'size' in the base class 'Form' and should be declared 'Shadows'.	C:\Users\Programming\Desktop\temp cleaner source\ACleaner\frmMain.Designer.vb	645	23	ACleaner

Warning	4	Function 'isTreeNodeChecked' doesn't return a value on all code paths. Are you missing a 'Return' statement?	C:\Users\Programming\Desktop\temp cleaner source\ACleaner\frmMain.vb	540	5	ACleaner


Just letting you know for information purposes really - but would be curious to know cause of these issues

----------


## weirddemon

> P.S. It appears to be coming from this line of code
> 
> 
> vb Code:
> Return getOS() & ", " & getProcessor() & ", " & returnSize(CInt(My.Computer.Info.TotalPhysicalMemory)) & ", " & GetGraphicsCardName()
> 
> I also get the following 4 warnings in VB:-
> 
> Warning	1	Variable 'osinfo' is passed by reference before it has been assigned a value. A null reference exception could result at runtime. Make sure the structure or all the reference members are initialized before use	C:\Users\Programming\Desktop\temp cleaner source\ACleaner\Classes\UniducksInformation.vb	86	34	ACleaner
> ...


The warnings are just warnings and are not causing any issues.

As for the exception, I'm not really sure. But if the error is indeed on that line, then the issue is most likely with the part where the method is converting the RAM. That class doesn't have anything to do with this project though. It was just added for looks, nothing more.

----------


## wingers

Okay thanks - only mentioned it as thought you might like to fix the issues

I also get the same issue another user had i.e. crashes when trying to access certain folders i.e. "Access to the path 'C:\Windows\CSC\v2.0.6' is denied."

Are you still working on this program, i.e. sorting out these errors - or not?

----------


## weirddemon

> Okay thanks - only mentioned it as thought you might like to fix the issues
> 
> I also get the same issue another user had i.e. crashes when trying to access certain folders i.e. "Access to the path 'C:\Windows\CSC\v2.0.6' is denied."
> 
> Are you still working on this program, i.e. sorting out these errors - or not?


Well, the error you mentioned doesn't really affect the functionality of the temp cleaning side, so I'm not worried about. You can just remove that line if you want.

As for that access exception, if haven't come up with a solution, but I have updated the project to just skip it. I haven't uploaded it in a while, so I'll do that.... probably tomorrow. I won't get out of work until sometime past midnight, so I won't have time until tomorrow after work.

----------


## wingers

Thanks - will look out for updated download tomorrow

----------


## wingers

REF: the exception I mentioned above


To fix this problem replace the line as shown below in the file UniducksInformation.vb:-

original line:-



```
Public Function GetComputerInformation() As String
Return getOS() & ", " & getProcessor() & ", " & returnSize(CInt(My.Computer.Info.TotalPhysicalMemory)) & ", " & GetGraphicsCardName()
End Function
```

fixed version:-



```
Public Function GetComputerInformation() As String
Return getOS() & ", " & getProcessor() & ", " & FormatNumber(My.Computer.Info.TotalPhysicalMemory / 1024 / 1024 / 1024, 2) & " Gb" & ", " & GetGraphicsCardName()
End Function
```

You can also then remove the section titled "Public Function returnSize"

----------


## wingers

> As for that access exception, if haven't come up with a solution, but I have updated the project to just skip it. I haven't uploaded it in a while, so I'll do that.... probably tomorrow. I won't get out of work until sometime past midnight, so I won't have time until tomorrow after work.


Sorry to chase - but please could you upload latest version as you said last week , or at least send me a copy of it if not

----------


## weirddemon

> Sorry to chase - but please could you upload latest version as you said last week , or at least send me a copy of it if not


Sorry about that. I've been so busy at work. The VP's seem to think that a 600K USD project can be completed in two weeks. So they've had us working ungodly hours  :Stick Out Tongue: 

Anyway, I've updated the original post with the new build. Everything should be good. Let me know if there's any problems.

----------


## wingers

> Sorry about that. I've been so busy at work. The VP's seem to think that a 600K USD project can be completed in two weeks. So they've had us working ungodly hours 
> 
> Anyway, I've updated the original post with the new build. Everything should be good. Let me know if there's any problems.


Excellent thank you

Yes that sounds about right - they always seem to think things can be done instantly!!!

----------


## wingers

hi weirddemon

have you done anymore work on this or your process manager

They are great apps and I have learnt a lot from looking at them and making my own versions as a practice

If you have - or have any other projects like this let me know

Cheers! and keep up the great work

----------


## weirddemon

> hi weirddemon
> 
> have you done anymore work on this or your process manager
> 
> They are great apps and I have learnt a lot from looking at them and making my own versions as a practice
> 
> If you have - or have any other projects like this let me know
> 
> Cheers! and keep up the great work


Nope. I've learned a lot since I first made it and I'm sure I could do a lot better. I laugh when I look back at that code. But I don't have time at the moment to work on it  :Stick Out Tongue:

----------


## wingers

hell, that was a quick response!

Fair enough

Don't suppose you have any other apps posted anywhere I can take a look at, to learn more?

----------


## weirddemon

> hell, that was a quick response!
> 
> Fair enough
> 
> Don't suppose you have any other apps posted anywhere I can take a look at, to learn more?


What is it that you have in mind? I have lots of little apps that I haven't posted.

----------


## wingers

anything really in the same sort of area as those two utilities - any sort of system utilities or admin utils - thats the sort of thing I am enjoying learning more about - and I seem to learn well by dissecting other apps

----------


## weirddemon

> anything really in the same sort of area as those two utilities - any sort of system utilities or admin utils - thats the sort of thing I am enjoying learning more about - and I seem to learn well by dissecting other apps


I'll look through my stuff tonight to see if I can find anything. You might also want to check out Chris128's stuff. He's made a lot IT related applications.

----------


## wingers

excellent - thank you - very much appreciated

Will also browse Chris128's stuff as you suggest

----------


## wader18

weirddemon pls sent working clean button codes man.

----------


## mmodelisi

I do not give an error message when I click on the clean button and the cleaning process .

----------


## mmodelisi

Eror Mesg

----------


## mmodelisi

Help Me ++++

----------


## mmodelisi

++++++++++++++++++++

----------

