Clearing History Items from the Environment using C#

In this blog we are going to clear the history items from the path.

Path of History:

C:\Users\laja\AppData\Local\Microsoft\Windows\History

Clearing History:

 

        /// <summary>

        /// Clears all the histories in the specific path.

        /// </summary>

        public void ClearHistory()

        {

            //Gets the Special Folder

            Environment.SpecialFolder specialFolder = Environment.SpecialFolder.History;

 

            //Gets the path of the special folder.

            string path = Environment.GetFolderPath(specialFolder);

 

            //Delete all the files from the histories.

            DeleteAllFiles(new DirectoryInfo(path));

 

            //Path of History cache

            Response.Write("<b>" + "HistoryPath" + " :</b> " + path + "<br/>");

        }

 

Deleting History:

//Delete all the files from the path.

        void DeleteAllFiles(DirectoryInfo diPath)

        {

            //Gets the files from the specific directory

            foreach (FileInfo fiCurrFile in diPath.GetFiles())

            {

                fiCurrFile.Delete();

            }

 

            //Getting Sub directories

            foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())

            {

                DeleteAllFiles(diSubFolder); // Call recursively for all subfolders

            }

        }

 

Thanks for reading this article. Have a nice day.

Ebook Download
View all
DevOps
Read by 0 people
Download Now!
Learn
View all