0

Method to clean Windows Temp folders.

Function C# 2 revisions 63 a month ago a month ago
static void Main(string[] args)
        {
            string userName = Environment.UserName;

            Console.WriteLine("Cleaning Windows/Temp...");
            try {
                System.IO.DirectoryInfo di = new DirectoryInfo("C:/Windows/Temp");
    
                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete(); 
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true); 
                }
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("Cleaning AppData/Local/Temp");

            try
            {
                System.IO.DirectoryInfo di = new DirectoryInfo("C:/Users/" + userName + "/AppData/Local/Temp");

                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete(); 
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true); 
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("All temporary files were deleted!");
}

Made in C#, this cleans:

  • Windows/Temp
  • AppData/Local/Temp