Wednesday, May 19, 2010

How to clear the temp folder in windows start up using C#


Today i decided to create an application using C# which clears the temp folder when ever windows startup.So no need to clear your temp file always.

Here is the code what i have tried:

RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
regKey.SetValue(Application.ProductName, Application.ExecutablePath);

string path = Path.GetTempPath();
if (!string.IsNullOrWhiteSpace (path))
DirectoryInfo d = new DirectoryInfo(path);
Array.ForEach(Directory.GetFiles(path), s =>
{ try { label1.Text = s; File.Delete(s); } catch (Exception ex) { } });
label1.Text = "Done";

If any exception will come it wont show that exception,it will proceed from the next

If put the application in "Software\Microsoft\Windows\CurrentVersion\Run" when ever the windows starts it automatically runs.