C# .NET - Clear temporary internet files via “C# winforms”

Asked By gopal krish on 24-Nov-11 07:39 AM

I am using webbrowser control in windows application.i was viewing url content inside the webbrowser control. here the url content values are stored in temporary internet files of user machine.

when i click close button in my form i was deleting the temporary internet files programatically. and it is working fine. but during deletion a "message box came and saying deleting temporary internet files".

here i dont want to show that dialog box during deletion. below is code to delete the temp files for users machine.

how to handle this..

void DeleteBrowserTempFile()
    {
        string args = "";
        args = ("InetCpl.cpl,ClearMyTracksByProcess 8");
        System.Diagnostics.Process process = null;
        System.Diagnostics.ProcessStartInfo processStartInfo;
        processStartInfo = new System.Diagnostics.ProcessStartInfo();
        processStartInfo.FileName =    Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\Rundll32.exe";
        if ((System.Environment.OSVersion.Version.Major >= 6))
        {
            //  Windows Vista or higher
            processStartInfo.Verb = "runas";
        }
        processStartInfo.Arguments = args;
        processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        processStartInfo.UseShellExecute = true;
        try
        {
            process = System.Diagnostics.Process.Start(processStartInfo);
        }
        catch (Exception ex)
        {
            Utilities.WriteErrorLog(ex);
        }
        finally
        {
            if (!(process == null))
            {
                process.Dispose();
            }
        }
    }

is there any other way to achieve this.. need ur suggestions with examples

regards
gopal.s

Jitendra Faye replied to gopal krish on 24-Nov-11 07:41 AM
Try this code-

string


path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)

//for deleting files

System.IO.

DirectoryInfo di = new DirectoryInfo(path);

foreach


(FileInfo file in di.GetFiles())

{

file.Delete();

}

foreach


(DirectoryInfo dir in di.GetDirectories())

{

dir.Delete(

true); //delete subdirectories and files

}



Try this and let me know.
Reena Jain replied to gopal krish on 24-Nov-11 07:51 AM
hi,

To clear session (such as HttpOnly cookies), you can use InternetSetOption() from wininet.dll.

private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
 
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

and use this method whenever need to clear session.

InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
webBrowser1.Document.Window.Navigate(url);

Hope this will help you
Riley K replied to gopal krish on 24-Nov-11 07:55 AM



You may have to clear the cache programmatically when your application hosts a WebBrowser control.
However, this feature is not available through the interfaces of the WebBrowser control.


WinInet Functions

To clear the cache directly, you can use the following WinInet functions:
  • Use the FindFirstURLCacheEntry function to find the first cache entry.
  • Use the FindNextUrlCacheEntry function to enumerate through the cache.
  • Use the DeleteUrlCacheEntry function to delete each entry.
The code sample in this article uses all of these functions.

http://

Regards
Devil Scorpio replied to gopal krish on 24-Nov-11 08:06 AM
Hi,

This C# code snippet deletes all the Temporary Internet files, i.e. clears the Internet Explorer cache.

using System.IO; 
...
void clearIECache()
{
   ClearFolder (new DirectoryInfo (Environment.GetFolderPath
      (Environment.SpecialFolder.InternetCache)));
}
 
void ClearFolder (DirectoryInfo folder)
{
   foreach (FileInfo file in folder.GetFiles())
      { file.Delete(); }
   foreach (DirectoryInfo subfolder in folder.GetDirectories())
      { ClearFolder(subfolder); }
}
 
public static void Main( )
{
   new Test().clearIECache ();
}