Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#

The Tiny Wrapper Class for URL History Interface in C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (22 votes)
5 Mar 2010CPOL5 min read 296.7K   7.7K   56   79
I think there are people who learned a bit of C# but don't know about COM. I hope my tiny wrapper class library is helpful for such people to use IUrlhistory interface without difficulty.
Sample Image - ponta.gif

Introduction

This article describes what the UrlHistoryWrapper class is and how to use it. The UrlHistoryWrapper class is a tiny class that wraps the C# equivalence of the IURLHistory interface in the MSDN document. They are wrapped into easy to use C# classes. This class provides the following features:

  1. Enumerate the items in the history cache.
  2. Place the specified URL into the history. If the URL does not exist in the history, an entry is created in the history. If the URL does exist in the history, it is overwritten.
  3. Clears history on a per-user basis.
  4. Queries the history and reports whether a particular URL has been visited by the current user.

To see these features in action, "start" or "start without debugging" the URLHIstoryDemo program from the Visual Studio .NET menu.

  1. You can see the URL history in the DataGrid. Push "Enumerate" button, and the program refreshes the URL history. When you type "file" in the TextBox at the top of the form, and then push "Enumerate" button, the program displays history items that match what you have typed into the TextBox (in this example, "file").
  2. When you type anything in the "URL" TextBox and the "Title" TextBox in the "Add or Delete URL" GroupBox, and then push "Add" button, the URL you typed is placed into the history. The new record is displayed at the bottom of the DataGrid.
  3. If you really really really want to clear the URL history except today's history, push "Clear All the History" button. This option will delete all the history except today's history, and "Temporary Internet Files" of your browser stores copies of all the pages you visit when you are surfing on the internet.
  4. When you type the URL in the "URL" TextBox in the "Query URL" GroupBox, and then push "Search" button, the program queries the history and reports whether a particular URL has been visited by the current user.

Using the Code (How to Use the UrlHistoryWrapper Class)

The UrlHistoryWrapper class resides in UrlHistoryLibrary namespace and is available in UrlHistoryLibrary.dll assembly. To add the class library to your project, follow these steps:

  1. In Solution Explorer, expand the project node you want to add a reference to.
  2. Right-click the References node for the project and select Add Reference from the shortcut menu.
  3. To add a reference to a component, do the following:
    1. In the Add Reference dialog box, select the tab indicating the Project.
    2. In the top pane, select the UrlHistoryLibrary.dll, and then click the Select button. If the UrlHistoryLibrary.dll is not in the list, you may locate it using the Browse button. The UrlHistoryLibrary.dll appears in the SelectedComponents pane of the dialog box. Click OK.
    3. UrlHistoryLibrary.dll will appear under the References node of the project.
  4. Add a using UrlHistoryLibrary; to your source code.
  5. Add a UrlHistoryWrapperClass field to your Form class with the following statement. Add a HistoryWrapperClass.STATURLEnumerator field to enumerate the items in the history cache and add an ArrayList field.
    C#
    UrlHistoryWrapperClass urlHistory;
    UrlHistoryWrapperClass.STATURLEnumerator enumerator;
    ArrayList list;
  6. In the constructor, add the following code after InitializeComponent call:
    C#
    //Initialize the UrlHistoryWrapperClass class
    urlHistory = new UrlHistoryWrapperClass();
    //Returns an enumerator that can iterate through the history cache.
    enumerator = urlHistory.GetEnumerator();
    list = new ArrayList();

To enumerate the items in the history cache and store them in the ArrayList object, call the GetUrlHistory method on the enumerator as follows:

C#
enumerator.GetUrlHistory(list);

The following code does the same thing:

C#
while(enumerator.MoveNext())
{
    list.Add(enumerator.Current);
}
enumerator.Reset();

By calling the SetFilter method on the enumerator before enumerating the items in the history cache with the following code, MoveNext() compares the specified URL with each URL in the history list to find matches. MoveNext() then copies the list of matches to a buffer. SetFilter method is used to specify the URL to compare.

C#
enumerator.SetFilter(textBoxFilter.Text , STATURLFLAGS.STATURLFLAG_ISTOPLEVEL);
while(enumerator.MoveNext())
{    
    list.Add(enumerator.Current);
}
enumerator.Reset();

UrlHistoryWrapperClass has GetEnumerator method. UrlHistoryWrapperClass.STATURLEnumerator class has MoveNext, Reset methods, and Current property. But these classes do not implement IEnumerable interface nor IEnumerator interface. The items in the history cache change often, and enumerator needs to reflect the data as it existed at a specific point in time. But Inside C# by Tom Archer does not recommend this approach.

To place the specified URL into the history, call the AddHistoryEntry method on the UrlHistoryWrapperClass object.

C#
urlHistory.AddHistoryEntry(textBoxURL.Text, 
  textBoxTitle.Text, ADDURL_FLAG.ADDURL_ADDTOHISTORYANDCACHE);

The first argument of this method is the string of the URL to place in the history. The second argument is the string of the title associated with that URL. The last argument is the flag which indicates where a URL is placed in the history.

To clear history on a per-user basis, call the ClearHistory method on the UrlHistoryWrapperClass object. This method will delete all the history except today's history, and Temporary Internet Files of your browser stores copies of all the pages you visit when you are surfing on the Internet.

C#
urlHistory.ClearHistory();

To query the history and report whether a particular URL has been visited by the current user, call the QueryURL method on the UrlHistoryWrapperClass object.

C#
STATURL s = urlHistory.QueryUrl(textBoxQueriedURL.Text, 
             STATURL_QUERYFLAGS.STATURL_QUERYFLAG_TOPLEVEL);
if(s.pwcsUrl != null)
{
    //Queried URL has been visited by the current user.
}
else 
{
    //Queried URL has not been visited by the current user.
}

The first argument of this method is the string of the URL to query. The second argument is one of the IUrlHistory flags. This method returns STATURL structure. If the returned STATURL's pwcsUrl is not null, the queried URL has been visited by the current user.

I deliberately avoided mentioning the DeleteHistoryEntry method, because it does not work. It is supposed to delete the specified URL from the history. That is why the "Delete" button on the demo application is disabled. I do not know how to work it out. I do not know whether the following link might help...

Points of Interest

I learned a bit of C# and the .NET Framework. The most annoying problem is that I have to use COM and Win32 API against my intention and without experience with COM and Win32API. As a matter of fact, I borrowed the type library from Eduardo A. Morcillo and referenced it in Visual Studio .NET and creates the Interop assembly. And then disassembled that Interop assembly. I used IL (Intermediate Language) in that Interop assembly file to write the source code in the file urlhist.cs". I think there are people who learned a bit of C# but don't know about COM. I hope my tiny class library is helpful for such people to use IUrlhistory interface without difficulty.

History

  • 22nd June, 2004: Initial post
  • 5th March, 2010: Updated the solution to .NET 3.5 version on Windows 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Thank you sir,you help me lot Pin
goblins2-Apr-08 14:12
goblins2-Apr-08 14:12 
GeneralValue does not fall within the expected range. Pin
beCognito5-Feb-07 20:25
beCognito5-Feb-07 20:25 
GeneralWin32api.cs(90,22): warning CS0618 under .NET V2.0 Pin
goblins28-Oct-05 14:18
goblins28-Oct-05 14:18 
Questionhow come when i clear all it doesnt clear it Pin
tayspen24-Sep-05 3:38
tayspen24-Sep-05 3:38 
AnswerRe: how come when i clear all it doesnt clear it Pin
goblins24-Sep-05 14:27
goblins24-Sep-05 14:27 
GeneralRe: how come when i clear all it doesnt clear it Pin
tayspen24-Sep-05 14:39
tayspen24-Sep-05 14:39 
GeneralRe: how come when i clear all it doesnt clear it Pin
goblins28-Sep-05 16:39
goblins28-Sep-05 16:39 
GeneralRe: how come when i clear all it doesnt clear it Pin
tayspen28-Sep-05 16:41
tayspen28-Sep-05 16:41 
Questionhow to make it work in a service? Pin
straywind2-Jun-05 21:20
straywind2-Jun-05 21:20 
AnswerRe: how to make it work in a service? Pin
goblins3-Jun-05 16:46
goblins3-Jun-05 16:46 
General"Eduardo A. Morcillo" link has changed. Pin
goblins3-Mar-05 0:57
goblins3-Mar-05 0:57 
GeneralRe: "Eduardo A. Morcillo" link has changed. Pin
unolegg22-Apr-05 23:54
unolegg22-Apr-05 23:54 
GeneralRe: "Eduardo A. Morcillo" link has changed. Pin
goblins24-Apr-05 16:00
goblins24-Apr-05 16:00 
GeneralDeleteHistoryEntry Pin
pusch3-Feb-05 2:05
pusch3-Feb-05 2:05 
GeneralRe: DeleteHistoryEntry Pin
goblins1-Mar-05 15:45
goblins1-Mar-05 15:45 
GeneralRe: DeleteHistoryEntry Pin
element1093-Nov-06 22:16
element1093-Nov-06 22:16 
GeneralDeleteHistoryEntry Pin
Anonymous3-Feb-05 2:01
Anonymous3-Feb-05 2:01 
QuestionWhere is UrlHistoryLibrary.dll?? Pin
surajguptha18-Jan-05 17:28
surajguptha18-Jan-05 17:28 
AnswerRe: Where is UrlHistoryLibrary.dll?? Pin
goblins1-Mar-05 15:47
goblins1-Mar-05 15:47 
GeneralRe: Where is UrlHistoryLibrary.dll?? Pin
Anonymous27-Mar-05 23:09
Anonymous27-Mar-05 23:09 
GeneralRe: Where is UrlHistoryLibrary.dll?? Pin
goblins30-Mar-05 2:07
goblins30-Mar-05 2:07 
GeneralRe: Where is UrlHistoryLibrary.dll?? Pin
James Ratcliff21-Aug-10 8:42
James Ratcliff21-Aug-10 8:42 
GeneralRe: Where is UrlHistoryLibrary.dll?? Pin
goblins22-Aug-10 3:27
goblins22-Aug-10 3:27 
GeneralChange to include deleting all history Pin
Richard J Slade24-Oct-04 4:50
Richard J Slade24-Oct-04 4:50 
GeneralRe: Change to include deleting all history Pin
goblins24-Oct-04 19:19
goblins24-Oct-04 19:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.