Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a form with a webKitBrowser. The user should surf on internet, and when he click a *.doc link, the word document must be shown. where can i tell the browser what to do when the user click on a .doc link?

i've done this (using System.Web.dll):
namespace testGoogleDoc
    public partial class Form1 : Form

        static bool IsDocumentToRedirect(string url)
            return url.ToLower().EndsWith(".doc") || url.ToLower().EndsWith(".docx");
        } //IsDocumentToRedirect

        public Form1()

            browser.Navigate("");  //BrowserHomePage

        private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
                string urlString = e.Url.ToString();

                if (urlString.ToLower().StartsWith("") || urlString.ToLower().StartsWith(""))
                    Uri uri = new Uri(urlString);
                    Uri doc = new Uri(HttpUtility.ParseQueryString(uri.Query).Get("q"));
                    urlString = doc.OriginalString.ToLower();
               if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith(""))
                    e.Cancel = true;
                    browser.Navigate("" +urlString);


is there a best way to extract the right URL in the query part of the other complexed URL?
Updated 12-May-13 1:31am
Sergey Alexandrovich Kryukov 10-May-13 13:54pm
You should not use such Regex, if any. You don't take into account Doc, dOC, docX, etc. You should use just doc|docx, but with case-insensitive Regex.

Why would you re-post almost identical question? You could better do it on the page of your original question. No matter, it's different. But your "doesn't work" is not informative...
Nnorss 10-May-13 14:03pm
ok for regex... i'm still searching for the solution for showing a word file on webKit browser, the biggest problem is that when i click on a .doc link nothing happens with webkitbrowser! i edited the "doesn't work" :D
Sergey Alexandrovich Kryukov 10-May-13 14:09pm
I got it. As I can see, you got to some alternative idea. It should work, only take into account my answer.

1 solution

I can see at least one problem: indirect recursion ([^]).

When you call Navigate, your browser gets to loading the another document, which leads to invocation of your Load event again. Recursion is "infinite". You need to isolate the case of navigation to "" from the navigation to a Word document. Also, you need to cancel navigation to a Word document.
Do something like
if (myBrowser.Url.ToString.ToLower().StartsWith("")) exit;

or something like that.

Also, fix your Regex as I explained in my comment to the question, please see. Or, simpler, use strong.ToLower and string.EndsWith, like:
string loUrl = myBrowser.Url.ToString.ToLower();
if (liUrl.endsWith(".docx") || liUrl.endsWith(".doc")) //...

Please see:[^].

This is not a final solution, but I think very close. Just work a bit in this direction.

By the way, I don't see a need in WebKit. If you work on Windows only, you can do the same thing with one of the WebBrowse classes already available in .NET FCL.

Nnorss 10-May-13 15:59pm
can you check my update please?
Sergey Alexandrovich Kryukov 10-May-13 16:16pm
No, this is not correct. Let's say, you have

You will try to navigate to it and, in the handler to (always add "http://"):

But this document ends on ".docx" anyway! So it will pass again, to:

Do you see the problem?

You should check up two things, not one:
1) if the document is doc/docx?
2) if the URL is already with the ""...

Got the idea? I actually already explained it in first place, in my answer.

Nnorss 10-May-13 16:42pm
i got it! thank you very much!
Sergey Alexandrovich Kryukov 10-May-13 16:46pm
Great! Hope you will be able to complete it.
Nnorss 10-May-13 16:48pm
I tried to show the clicked link of the browser in a label. Every time the user click on a link, the link is shown in the label, exept when i click on a *.doc, the value of the label dont change and nothing happen! I know It will be more easy with the default browser, i changed to web kit because it was ver laggy and it freeze the whole application when the user is surfing. can you help me with this please??
Sergey Alexandrovich Kryukov 10-May-13 16:56pm
I need some thinking on further detail of this implementation... probably you need to use one more event or simply invocation of the call to Navigate (like Dispatcher.Invoke or System.Windows.Forms.Control.Invoke)...
Nnorss 10-May-13 17:04pm
ok tell me if you catch something interesting about that
Sergey Alexandrovich Kryukov 10-May-13 17:07pm
It just need some time to play around. Please do it, and I'll keep it in mind, too.
Nnorss 10-May-13 17:17pm
i'm really blocked! i tried several codes.. should i open a new discussion about this?
Sergey Alexandrovich Kryukov 10-May-13 17:27pm
You are not supposed to. If you update a question, your post will get back to the top of the list again.
I suggest you publish your newest fixed code using "Improve question" (please comment this comment, so I would give a notification)...
And, did you try my variant with Invoke?
Also, you need to use the debugger.
Nnorss 10-May-13 17:31pm
i try to delete the new post but red error message "insufficient permission to delete"
Sergey Alexandrovich Kryukov 10-May-13 19:15pm
No problem; it's probably presently disabled, even for your own posts...

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900