Home > WinForms > C#: WinForms – Manage Cache state with Webclient CachePolicy

C#: WinForms – Manage Cache state with Webclient CachePolicy

17:49

Marek Śliwiński Leave a comment Print This Post  (299) Go to comments

When using Webclient to retrieve some file from the web you can face problem with caching your files.

For example: You have a xml file with version number inside. After downloading first time by Webclient class, you changed content of the xml file and start process again. Unfortunately you will receive old content downloaded earlier instead newest content of the file.

That happens because of Microsoft Internet Explorer (as far as I know) Cache settings used by Webclient class.

So to disallow for caching your requests you have to change Weblicent CachePolicy property. You can do it as shown below:

string xmlUrl = "http://myserver.com/xmlfile.xml";

WebClient client = new WebClient();

// prevent file caching by windows
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(
System.Net.Cache.RequestCacheLevel.NoCacheNoStore
);

// read content of file
Stream rssStream = client.OpenRead(xmlUrl);
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,
  1. September 4th, 2009   (Quote) at 12:21   (Quote) | #1

    Very nice tip indeed!

    I do not know how many people did find it – http://silverlight.net/forums/t/14453.aspx

    regards,

    Digvijay

  1. No trackbacks yet.

Subscribe without commenting