Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seach folder structure and display structure on page...sounds cool!!

Status
Not open for further replies.

echowave

Programmer
Jan 15, 2002
13
CA
I currently have a client whose webpage contains multiple hyperlinks to folders on their network. Unfortunately, these links are quite static and the folder structure is always changing, therefore a real pain in the butt for support.

What I would like to accomplish is upon loading of the web page, read the network folder structure and display the folders that way. I only need to read one folder and one level of subfolders(would be the headers). I am not sure what the best way to display the folders on the page though. I could use hyperlinks, but if subdirectories are contantly growing, I could end up with 50 hyplerlinks on the page. This does not seem impossible to say the least, but I am looking for the best way to do this.

Does anyone have any suggestions? If you any further questions, please feel free to ask them, as I would be more than happy to answer them.

thanks in advance!!


 
There's recursive functions all over the place that can traverse folder structures. You could use one of these functions to traverse a folder structure and selectively write html.
 
i once wrote this in a jiffy, its dirty but it works ( in the codebehind)

the aspx page :

Code:
...
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> 
...
<P align="left">				<iewc:TreeView id="TreeView1" runat="server"></iewc:TreeView><IMG height="1" src="../images/general/space.gif" width="600"></P>
...

the codebehind :

Code:
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;

namespace HelloWorldMyTekTipsSample
{         
	
	/// <summary>
	/// Summary description for this demo.
	/// </summary>
	public class BrowseNetworkStuff : System.Web.UI.Page
	{

protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
		protected System.Web.UI.WebControls.Label statusMessage;
	

		private void FillTree(string currentDir, Microsoft.Web.UI.WebControls.TreeNode node) {
			string[] dirList = System.IO.Directory.GetDirectories(currentDir);
			string[] fileList = System.IO.Directory.GetFiles(currentDir);
			int	amountOfDirs;
			int amountOfFiles;

			// Fill the Tree with dirs from the current directory
			foreach (string dir in dirList) {
				Microsoft.Web.UI.WebControls.TreeNode dirNode = new Microsoft.Web.UI.WebControls.TreeNode();
				string[] finalDir = dir.ToString().Split('\\');				
				dirNode.Text = finalDir.GetValue(finalDir.Length-1).ToString();				
				string[] finalDir2 = dirNode.Text.ToString().Split('/');
				dirNode.Text = finalDir2.GetValue(finalDir2.Length-1).ToString();				

				//dirNode.Text = dir.ToString();
				dirNode.Expanded = false;
				dirNode.Expandable = Microsoft.Web.UI.WebControls.ExpandableValue.Always;
				dirNode.ImageUrl = HttpContext.Current.Server.MapPath("/webctrl_client/1_0/Images/folder.gif");
				dirNode.ExpandedImageUrl = HttpContext.Current.Server.MapPath("/webctrl_client/1_0/Images/folderopen.gif");
				node.Nodes.Add(dirNode);				
				amountOfDirs = System.IO.Directory.GetDirectories(dir.ToString() + '/').Length;
				amountOfFiles = System.IO.Directory.GetFiles(dir.ToString() + '/').Length;
				if (amountOfDirs > 0 || amountOfFiles > 0) {					
					FillTree(dir.ToString() + '/', dirNode);
				}
			}

			// Fill the Tree with files from the current directory
			foreach (string fil in fileList) {
				Microsoft.Web.UI.WebControls.TreeNode fileNode = new Microsoft.Web.UI.WebControls.TreeNode();
				
				string[] finalFile = fil.ToString().Split('/');
				fileNode.Text = finalFile.GetValue(finalFile.Length-1).ToString();
				fileNode.NavigateUrl=fil.ToString();
				fileNode.Expanded = false;
				fileNode.Expandable = Microsoft.Web.UI.WebControls.ExpandableValue.Auto;
				fileNode.ImageUrl = HttpContext.Current.Server.MapPath("/webctrl_client/1_0/Images/html.gif");
				node.Nodes.Add(fileNode);
			}
		}


		private void FillTreeRoot() {
			//
			//
			ApplyStyle(TreeView1);
			string rootDir = HttpContext.Current.Server.MapPath("/SomeRootDirIfYouNeedIt/");
			Microsoft.Web.UI.WebControls.TreeNode node = new Microsoft.Web.UI.WebControls.TreeNode();				
			node.Text = "Hello World";
			node.Expanded = true;
			node.ImageUrl = HttpContext.Current.Server.MapPath("/webctrl_client/1_0/Images/folder.gif");
			node.ExpandedImageUrl = HttpContext.Current.Server.MapPath("/webctrl_client/1_0/Images/folderopen.gif");
			TreeView1.Nodes.Add(node);
			FillTree(rootDir, node);
			
		}

		private void ApplyStyle(Microsoft.Web.UI.WebControls.TreeView TreeView) {
			TreeView.Style.Clear();
			TreeView.DefaultStyle.Add("font-family", "Verdana");
			TreeView.DefaultStyle.Add("font-size", "10pt");
			TreeView.DefaultStyle.Add("background-color", "#ffffff"); 
			TreeView.SelectedStyle.Add("color", "black");
			TreeView.SelectedStyle.Add("background-color", "#ffffff"); 

		
			Microsoft.Web.UI.WebControls.CssCollection style = new Microsoft.Web.UI.WebControls.CssCollection();
			style.Add("color" ,"#ff6600");
			TreeView.HoverStyle = style;

			string m_ButtonLinkStyle = "this.style.color='#000000'";
			string m_ButtonLinkHoverStyle = "this.style.color='#ff6600'";

			TreeView.HoverStyle.Add("onmouseover", m_ButtonLinkHoverStyle);
			TreeView.HoverStyle.Add("onmouseout", m_ButtonLinkStyle);

			TreeView.ShowLines = false;
			
			TreeView.SystemImagesPath = "/images/TreeControl";
			TreeView.Nodes.Clear();
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here

			FillTreeRoot();			

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

--------------------------------------
deleau@gmail.com
 
Thanks for all of the replies (and code!!). Greatly appreciated!!
 
Hi,

I am trying to create a button that when clicked, opens up a windows for you to select a file on a network. I doesnt have to have the ability to open the file, just select it.
(I would work like the CommonDialogBox would work on VB6)

Has anybody got any code that does this ?
I want it to work on ASP.NET with VB.NET as the language


Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top