Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to drag a link from a browser to my application and pick up both the url and the title/name associated with it.
I can read the url easily enough:
C#
object o = data.GetData(typeof(string));
if (o != null)
{
    if (data.GetDataPresent(DataFormats.Html, true))
    {
       String htmlsnippet = (string)data.GetData(DataFormats.Html);
       processHtml(htmlsnippet); // test code that looks at the html contents
       pasteText((String)o);     // pastes the url into my app - want title too!


The problem is that the html rarely contains the title.
For example www.bbc.co.uk/news
drag this into my app and I see the url
and the htmp snippet is:

HTML
Version:0.9StartHTML:00000145EndHTML:00000282
StartFragment:00000179EndFragment:00000246
SourceURL:chrome://browser/content/browser.xul<html><body>
<!--StartFragment-->
<a href=\"http://www.bbc.co.uk/news/\">http://www.bbc.co.uk/news/</a><!--EndFragment-->
</body></html>


However if I drag this link from my browser to my desk top the short-cut is
called 'BBC News - Home'

Similarly try cnn.com and the shortcut is labelled 'CNN.com International -
Breaking, World ...'

Can anyone help me find the title information? I don't want to retrieve the page as this will slow down the drag and drop, and anyway drag to desktop doesn't do this since it works if you go off-line and then drag a link from the browser to desktop.
Thanks.

[Modified: added pre tags...learn to use them, they are your friend!]
Posted
Updated 15-Sep-10 6:22am
v2

1 solution

C#
MemoryStream fgdStream = (MemoryStream)e.Data.GetData("FileGroupDescriptor");
byte[] fgdBytes = new byte[fgdStream.Length];
fgdStream.Read(fgdBytes, 0, fgdBytes.Length);
fgdStream.Close();

StringBuilder fileName = new StringBuilder();

for (int i = 76; i < fgdBytes.Length; i++)
{
    if (fgdBytes[i] != 0)
    {
        fileName.Append(Convert.ToChar(fgdBytes[i]));
    }
    else
    {
        break;
    }
}
MessageBox.Show(fileName.Remove(fileName.Length - 4,4).ToString());


(the Remove part is because the name ends in ".URL")
 
Share this answer
 
Comments
Chrysogon 15-Sep-10 13:26pm    
That works great. Thank you so much!
Chrysogon 16-Sep-10 2:33am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

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