-1

I tried to

System.IO.DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));

foreach (FileInfo file in di.GetFiles())
{
    file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true);
}

and

System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8");

.

First one is not working.

The other method works, but there are issues with compatibility and loading windows.

Is there another good way?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Hans
  • 59
  • 2
  • 10
  • 1
    Please don't add unnecessary tags. From the information you have provided, this question isn't remotely related to WinForms. – ProgrammingLlama Nov 26 '19 at 08:23
  • 3
    "*First one is not working.*" is not a technical description of a problem. – TheGeneral Nov 26 '19 at 08:24
  • "Environment.SpecialFolder.InternetCache" path requires some kind of permission. Did you check it? – Dilshod K Nov 26 '19 at 08:27
  • related: https://stackoverflow.com/questions/5274397/clearing-ie-cache-programmatically-vs-inetcpl-cpl-clearmytracksbyprocess?noredirect=1&lq=1 , https://stackoverflow.com/questions/16167984/deleting-ie-cache-and-cookies-using-c-sharp-code-in-wpf , – Drag and Drop Nov 26 '19 at 08:36
  • The first code builds successfully but the form doesn't pop up. – Hans Nov 26 '19 at 08:52
  • I didn't check the permissions, but I verified that the path was properly received through the messagebox. thanks I'll check. – Hans Nov 26 '19 at 08:54
  • The first method will not work if the files are opened or in use by some process. It will raise an error. Can you please inform us with some more detailed, what issue you are having with second way? – Deepak-MSFT Nov 26 '19 at 12:54
  • Thanks, you are right. I was using it in another process, so I got the error. The second way is to not delete only certain files. – Hans Nov 27 '19 at 02:16
  • Looks like you want to keep some files. If you know the file name that you want to keep then try to check and match the file name while deleting the files using first approach. – Deepak-MSFT Nov 27 '19 at 08:55

1 Answers1

-1

The first method is correct and there's nothing wrong about it. But if you want maybe something that "looks" cleaner, while it does the same thing, you can do something like this:

  • Encapsulate the cleaning logic in an extension method
  • Call the extension method on the directory.

An example approach can be found as an answer to a similar post here

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Jimi
  • 1,605
  • 1
  • 16
  • 33