Click here to Skip to main content
15,886,578 members
Articles / Web Development / ASP.NET

URL sharing across a network with ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.89/5 (16 votes)
3 Sep 20037 min read 194.3K   2K   75  
This ASP.NET application allows the sharing of Internet Explorer Favorites URLs over an intranet or the Internet.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
using System.Runtime.InteropServices;

namespace jm.Bookmarks
{
	/// <summary>
	/// Zusammendfassende Beschreibung f�r BookmarksTop.
	/// </summary>
	public class BookmarksTop : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal Literal1;
		protected System.Web.UI.WebControls.TextBox tbxPath;
		
		//Xml document variable
		private	XmlDocument _doc;
		//
		public string cFavoritesPath = @"E:\Documents and Settings\JMC\Favorites";
		protected System.Web.UI.WebControls.Button btnUpdate;
		public string cRootFolderName = "root";
	
		//This "old" Windows function allows us to read the content of the .url files
		[DllImport("KERNEL32.DLL",EntryPoint="GetPrivateProfileString")]
		protected internal static extern int GetPrivateProfileString(string lpAppName, 
			string lpKeyName, string lpDefault, 
			StringBuilder lpReturnedString, int nSize, 
			string lpFileName);

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack) 
			{
				tbxPath.Text = cFavoritesPath;			
				tbxPath.Font.Name = "verdana";
				//Ensure that the path is ended with a backslash "\"
				if (!cFavoritesPath.EndsWith(@"\"))
					cFavoritesPath += @"\";				
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: Dieser Aufruf ist f�r den ASP.NET Web Form-Designer erforderlich.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Erforderliche Methode f�r die Designerunterst�tzung. 
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor ge�ndert werden.
		/// </summary>
		private void InitializeComponent()
		{    
			this.tbxPath.TextChanged += new System.EventHandler(this.tbxPath_TextChanged);
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void tbxPath_TextChanged(object sender, System.EventArgs e)
		{
			cFavoritesPath = tbxPath.Text;
			//Ensure that the path is ended with a backslash "\"
			if (!cFavoritesPath.EndsWith(@"\"))
				cFavoritesPath += @"\";
		}

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			//Get the path from the textbox
			cFavoritesPath = tbxPath.Text;

			//Ensure that the path is ended with a backslash "\"
			if (!cFavoritesPath.EndsWith(@"\"))
				cFavoritesPath += @"\";		

			//Get the root folder's name
			string cTemp = cFavoritesPath.Substring(0,cFavoritesPath.Length-1);
			int nLastIndex = cTemp.LastIndexOf(@"\");
			cRootFolderName = cTemp.Substring(nLastIndex+1);
			
			//Read all files from a given folder			
			if (Directory.Exists(cFavoritesPath))
			{
				Literal1.Text = "";
				//Create an XML document
				_doc = new XmlDocument();
				_doc.LoadXml("<folder></folder>");
				XmlNode root = _doc.DocumentElement;
				newElement(root,cRootFolderName, cFavoritesPath);
				GetFiles(root, cFavoritesPath);

				//Generate the XML declaration
				XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0","iso-8859-1",null);				
				_doc.InsertBefore(xmldecl, root);

				//Fill the XmlDocument with elements "folder" & "file"
				GetFolders(root,cFavoritesPath);
				
				//Save the xml document
				try
				{					
					_doc.Save(Server.MapPath("Bookmarks.xml"));			
				}
				catch (Exception ex)
				{
					Literal1.Text = ex.Message;
				}
			} 
			else
			{
				Literal1.Text = "The given path is not available.";
			}
		}

		private void GetFolders(XmlNode parentNode, string parentPath)
		{
			string [] subdirectoryEntries = Directory.GetDirectories(parentPath);
			foreach(string subdirectory in subdirectoryEntries)
			{
				int index = subdirectory.LastIndexOf(@"\");
				string cRootFolderName = subdirectory.Substring(index+1,subdirectory.Length-index-1);
				string cRelativePath = subdirectory.Substring(cFavoritesPath.Length);
				XmlElement elem = _doc.CreateElement("folder");
				XmlNode newChild = parentNode.AppendChild(elem);
				newElement(newChild, cRootFolderName, cRelativePath);
				GetFiles(newChild, subdirectory);
				GetFolders(newChild, subdirectory);
			}
		}

		private void GetFiles(XmlNode parentNode, string parentPath)
		{
			string [] fileEntries = Directory.GetFiles(parentPath);
			foreach(string fileName in fileEntries)
			{
				//If the file is a web shortcut, get the URL				
				
				//Check the extension of the file
				string cURL = "";
				int DotIndex = fileName.LastIndexOf(@".");
				string cExtension = fileName.Substring(DotIndex+1,fileName.Length-DotIndex-1);
				if (cExtension.CompareTo("url") == 0)
				{	
					StringBuilder buffer = new StringBuilder(256);
					if (buffer != null) 
					{
						int bufLen = GetPrivateProfileString("InternetShortcut", "URL", "", buffer, buffer.Capacity, fileName);
						cURL = buffer.ToString();

						//Get the name to be displayed: remove the full path & the '.url' extension
						int SlashIndex = fileName.LastIndexOf(@"\");
						string cDisplayName = fileName.Substring(SlashIndex+1,fileName.Length-SlashIndex-5);
						XmlElement elem = _doc.CreateElement("file");
						XmlNode newChild = parentNode.AppendChild(elem);
						newElement(newChild, cDisplayName, cURL);				
					}
				}
			}
		}

		//Create a new element in the XML file
		private void newElement(XmlNode parentNode, string Attributename, string Attributepath)
		{		
			XmlNode attrName = _doc.CreateNode(XmlNodeType.Attribute, "name", "");
			attrName.Value = Attributename;
			parentNode.Attributes.SetNamedItem(attrName);

			XmlNode attrPath = _doc.CreateNode(XmlNodeType.Attribute, "path", "");
			attrPath.Value = Attributepath;
			parentNode.Attributes.SetNamedItem(attrPath);
		}		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
Jean-Michel is a Swiss citizen living in France in the georgious county of Alsace.
Mastering french, english and german, he is open to the world and has a strong anger to learn and discover new horizons.
In the computer science since 20 years (the first computer was a ZX81 self-mounted Sinclair), Jean-Michel is a recent MCAD for .NET certified.
He enjoys sailing any time he does not code.

Comments and Discussions