Click here to Skip to main content
11,701,590 members (66,622 online)
Click here to Skip to main content

The Tiny Wrapper Class for URL History Interface in C#

, 5 Mar 2010 CPOL 181.5K 5.3K 52
Rate this:
Please Sign up or sign in to vote.
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.
    UrlHistoryWrapperClass urlHistory;
    UrlHistoryWrapperClass.STATURLEnumerator enumerator;
    ArrayList list;
  6. In the constructor, add the following code after InitializeComponent call:
    //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:

enumerator.GetUrlHistory(list);

The following code does the same thing:

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.

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.

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.

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.

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)

Share

About the Author

goblins
Japan Japan
No Biography provided

You may also be interested in...

Comments and Discussions

 
Bugit throws an exception and can't compile as it is Pin
Grumpyman22-Mar-15 4:41
memberGrumpyman22-Mar-15 4:41 
SuggestionRe: it throws an exception and can't compile as it is Pin
goblins22-Mar-15 5:25
membergoblins22-Mar-15 5:25 
QuestionNice to read this article. If browser history off then can we get visited url's? Pin
amit_832-Feb-14 18:16
memberamit_832-Feb-14 18:16 
QuestionRe: Nice to read this article. If browser history off then can we get visited url's? Pin
goblins3-Feb-14 1:36
membergoblins3-Feb-14 1:36 
QuestionHow to get all browser history Pin
Balasubramanian T28-Oct-13 21:48
memberBalasubramanian T28-Oct-13 21:48 
AnswerRe: How to get all browser history Pin
goblins28-Oct-13 23:30
membergoblins28-Oct-13 23:30 
QuestionHi goblins Pin
dev_elkerdawy31-Aug-13 8:03
memberdev_elkerdawy31-Aug-13 8:03 
AnswerRe: Hi goblins Pin
goblins31-Aug-13 13:31
membergoblins31-Aug-13 13:31 
QuestionPlease help me Pin
dev_elkerdawy31-Aug-13 5:33
memberdev_elkerdawy31-Aug-13 5:33 
AnswerRe: Please help me Pin
goblins31-Aug-13 6:05
membergoblins31-Aug-13 6:05 
QuestionRun as Local System Pin
Agnel Antony20-Sep-12 10:14
memberAgnel Antony20-Sep-12 10:14 
AnswerRe: Run as Local System Pin
goblins21-Sep-12 3:01
membergoblins21-Sep-12 3:01 
GeneralSystem.Runtime.InteropServices.COMException (0x80070008) Pin
Member 91698535-Aug-12 11:09
memberMember 91698535-Aug-12 11:09 
GeneralRe: System.Runtime.InteropServices.COMException (0x80070008) Pin
Member 91698539-Aug-12 2:58
memberMember 91698539-Aug-12 2:58 
GeneralRe: System.Runtime.InteropServices.COMException (0x80070008) Pin
goblins9-Aug-12 16:26
membergoblins9-Aug-12 16:26 
GeneralRe: System.Runtime.InteropServices.COMException (0x80070008) Pin
Member 916985310-Aug-12 9:53
memberMember 916985310-Aug-12 9:53 
Ok, thanks for your help anyways.
QuestionIs it can used in a win 8 app? Pin
RickWang201212-Apr-12 23:08
memberRickWang201212-Apr-12 23:08 
AnswerRe: Is it can used in a win 8 app? Pin
goblins13-Apr-12 3:06
membergoblins13-Apr-12 3:06 
GeneralRe: Is it can used in a win 8 app? Pin
RickWang201216-Apr-12 21:54
memberRickWang201216-Apr-12 21:54 
QuestionWill it work with browser other than IE? Pin
Navin Pandit24-Jan-12 21:36
memberNavin Pandit24-Jan-12 21:36 
QuestionHi Need some help! Pin
Andrew D Sullivan9-Jan-12 21:30
memberAndrew D Sullivan9-Jan-12 21:30 
AnswerRe: Hi Need some help! Pin
goblins11-Jan-12 15:04
membergoblins11-Jan-12 15:04 
QuestionHow can I get the hit number of URLs in history? Pin
theodolite21-Oct-11 0:12
membertheodolite21-Oct-11 0:12 
GeneralRe: How can I get the hit number of URLs in history? Pin
goblins21-Oct-11 2:18
membergoblins21-Oct-11 2:18 
Questionhow could i send list through LAN using WCF Pin
Member 79079309-Sep-11 7:04
memberMember 79079309-Sep-11 7:04 
AnswerRe: how could i send list through LAN using WCF [modified] Pin
goblins10-Sep-11 2:17
membergoblins10-Sep-11 2:17 
GeneralRe: how could i send list through LAN using WCF [modified] Pin
Member 790793011-Sep-11 9:40
memberMember 790793011-Sep-11 9:40 
GeneralRe: how could i send list through LAN using WCF Pin
goblins13-Sep-11 2:30
membergoblins13-Sep-11 2:30 
Generalwhere is the f***ing UrlHistoryLibrary.dll Pin
MustangU28-Jan-11 1:05
memberMustangU28-Jan-11 1:05 
GeneralRe: where is the f***ing UrlHistoryLibrary.dll Pin
Tassaduq00926-Apr-11 4:06
memberTassaduq00926-Apr-11 4:06 
QuestionWhich browser it logs? Pin
Member 74095413-Nov-10 17:56
memberMember 74095413-Nov-10 17:56 
AnswerRe: Which browser it logs? Pin
goblins4-Nov-10 1:28
membergoblins4-Nov-10 1:28 
GeneralRe: Which browser it logs? Pin
Member 74095414-Nov-10 7:30
memberMember 74095414-Nov-10 7:30 
GeneralRe: Which browser it logs? Pin
goblins5-Nov-10 1:31
membergoblins5-Nov-10 1:31 
Generalproblem when loading to the server Pin
eyal moshkovich29-May-10 9:37
membereyal moshkovich29-May-10 9:37 
AnswerRe: problem when loading to the server Pin
goblins29-May-10 15:30
membergoblins29-May-10 15:30 
GeneralGreat thanks + Sort by visit Pin
eyal moshkovich26-May-10 8:58
membereyal moshkovich26-May-10 8:58 
Generalin a server enviroment Pin
cholo12347-Jan-10 7:15
membercholo12347-Jan-10 7:15 
AnswerRe: in a server enviroment Pin
goblins11-Jan-10 23:34
membergoblins11-Jan-10 23:34 
GeneralUrlHistoryWrapperClass not getting Browser history [modified] Pin
Mohammed shakeel Khan5-Jul-09 20:33
memberMohammed shakeel Khan5-Jul-09 20:33 
AnswerRe: UrlHistoryWrapperClass not getting Browser history Pin
goblins6-Jul-09 4:01
membergoblins6-Jul-09 4:01 
GeneralRe: UrlHistoryWrapperClass not getting Browser history Pin
Mohammed shakeel Khan6-Jul-09 19:44
memberMohammed shakeel Khan6-Jul-09 19:44 
GeneralThank you sir,you help me lot Pin
wosi1-Apr-08 14:50
memberwosi1-Apr-08 14:50 
GeneralRe: Thank you sir,you help me lot Pin
goblins2-Apr-08 14:12
membergoblins2-Apr-08 14:12 
GeneralValue does not fall within the expected range. Pin
beCognito5-Feb-07 20:25
memberbeCognito5-Feb-07 20:25 
GeneralWin32api.cs(90,22): warning CS0618 under .NET V2.0 Pin
goblins28-Oct-05 14:18
membergoblins28-Oct-05 14:18 
Questionhow come when i clear all it doesnt clear it Pin
tayspen24-Sep-05 3:38
membertayspen24-Sep-05 3:38 
AnswerRe: how come when i clear all it doesnt clear it Pin
goblins24-Sep-05 14:27
membergoblins24-Sep-05 14:27 
GeneralRe: how come when i clear all it doesnt clear it Pin
tayspen24-Sep-05 14:39
membertayspen24-Sep-05 14:39 
GeneralRe: how come when i clear all it doesnt clear it Pin
goblins28-Sep-05 16:39
membergoblins28-Sep-05 16:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 5 Mar 2010
Article Copyright 2004 by goblins
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid