Recursive FTP Treeview

I need to find out how to bypass this one ...

Page 1 of 1

0 Replies - 4233 Views - Last Post: 15 January 2009 - 05:13 AM Rate Topic: -----

#1 crazyvonzipper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 09

Recursive FTP Treeview

Post icon  Posted 15 January 2009 - 05:13 AM

Please check out my code

void recurese(string root)
		{
			StringBuilder result = new StringBuilder();
			FtpWebRequest reqFTP;
			root = "ftp://ngaleafrica.co.za";
			try
			{
				reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
				reqFTP.UseBinary = true;
				reqFTP.Credentials = new NetworkCredential(txtUser.text, txtPass.text);
				reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
				WebResponse response = reqFTP.GetResponse();
				StreamReader reader = new StreamReader(response.GetResponseStream());

				string line = reader.ReadLine();
				while (line != null)
				{
					result.Append(line);
					result.Append("\n");
					line = reader.ReadLine();
				}
				result.Remove(result.ToString().LastIndexOf('\n'), 1);
				reader.Close();
				response.Close();

				string[] Result =  result.ToString().Split('\n');

				if (Result != null)
				{
					foreach (string ParentDir in Result)
					{
						TreeNode ParentNode = new TreeNode(ParentDir);
						txtShowDirectory.Text = root + "/" + ParentDir;
						Console.WriteLine(txtShowDirectory.Text);
						treeView1.Nodes.Add(ParentNode);
						treeView1.Refresh();
						Console.WriteLine("Adding Parent node : " + ParentNode.Text);
						recureseChild(ParentNode, root + "/" + ParentDir);
					}
				}
				
			}
			catch (Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message);
			}
		}

		void recureseChild(TreeNode Node, string root)
		{
			StringBuilder result = new StringBuilder();
			FtpWebRequest reqFTP;
			root = "ftp://ngaleafrica.co.za/" + txtShowDirectory.Text + "/";
			try
			{
				reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
				reqFTP.UseBinary = true;
				reqFTP.Credentials = new NetworkCredential(txtUser.text, txtPass.text);
				reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;


			   [i][/color] WebResponse response = reqFTP.GetResponse()[color=#FF0000];[/i][u][color=#FF0000]

					StreamReader reader = new StreamReader(response.GetResponseStream());

					string line = reader.ReadLine();
					while (line != null)
					{
						result.Append(line);
						result.Append("\n");
						line = reader.ReadLine();
					}
					result.Remove(result.ToString().LastIndexOf('\n'), 1);
					reader.Close();
					response.Close();

					string[] Result = result.ToString().Split('\n');

					if (Result != null)
					{
						foreach (string ChildDir in Result)
						{
							TreeNode ChildNode = new TreeNode(ChildDir);
							Console.WriteLine("Start Childnode :" + ChildNode.Text);
							Node.Nodes.Add(ChildNode);

							recureseChild(ChildNode, root + "/" + ChildDir);
						}
					}
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failure : " + ex.Message);
			}
		}
 


I need to get passed that little problem , (problem = Skipping the webresponse if it tries to get a file in stead of a directory...)

Any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Page 1 of 1