I am making a recursive treeview that lists the directories on my server [or trying atleast]...
So far so good , it seems to work, cept it does not load all the files / directories, please consider my code :
void recurese(string root)
{
// New instance of the StringBuilder Class (resluts)
StringBuilder result = new StringBuilder();
// New instance of FtpWebRequest (reqFTP)
FtpWebRequest reqFTP;
// Setting root to ftp://ngaleafrica.co.za
root = "ftp://" + txtFtpServerName.Text;
try
{
// Ceating URI for reqFTP , Setting URI to ROOT
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
// Setting Binary to true
reqFTP.UseBinary = true;
// Setting credentials for reqFTP
reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.Text);
// Setting method of reqFTP to get a list of directories
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
// Creating FtpWebResponse, response gets the webrequest
WebResponse response = reqFTP.GetResponse();
// Creating new stream reader to read the response
StreamReader reader = new StreamReader(response.GetResponseStream());
// Creatinf string (line) that reads the streamreader lines
string line = reader.ReadLine();
// While the line is not null
while (line != null)
{
// StringBuilder adds line (the streamreader line)
result.Append(line);
// Add a line break to the line
result.Append("\n");
// telling line to read another line of the streamreader
line = reader.ReadLine();
}
// Removing last linebreak from Stringbuilder...
result.Remove(result.ToString().LastIndexOf('\n'), 1);
// Closing the StremReader
reader.Close();
// Closing FtpWebResponse
response.Close();
// Creating array RESULT
// Splitting Streamreader string where linebreak occurs
string[] Result = result.ToString().Split('\n');
// If Result is not null or void whatever
if (Result != null)
{
// Foreach string in the string array Result
foreach (string ParentDir in Result)
{
// Add Parent Node to hold values for strings (first level)
TreeNode ParentNode = new TreeNode(ParentDir);
if (ParentDir.Contains("."))
{ }
else
{
// Adding ParentNode to treeview
treeView1.Nodes.Add(ParentNode);
// Showing the Directory ... not really important
txtShowDirectory.Text = root + "/" + ParentDir;
// Showing the Directory ... not really important
Console.WriteLine("Txt Show Dir : " + root + "/" + ParentDir);
// Show the parentnode text
Console.WriteLine("Adding Parent node : " + ParentNode.Text);
// Setting bool Isfile to false
Isfile = false;
// Calling recurse child method
recureseChild(ParentNode, root + "/" + ParentDir);
}
}
// Refreshing the treeview
treeView1.Refresh();
}
}
catch (Exception ex)
{
Console.WriteLine("Error in 'recurse' : " + ex.Message);
}
}
void recureseChild(TreeNode Node, string root)
{
if (Isfile == false)
{
// New instance of the StringBuilder Class (resluts)
StringBuilder result = new StringBuilder();
// New instance of FtpWebRequest (reqFTP)
FtpWebRequest reqFTP;
root = root;
Console.WriteLine("Child Root : " + root);
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(root));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(txtFtpUserName.Text, txtFtpPassword.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 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 in Recurse Child : " + ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
recurese(root);
}
The problem lies here
foreach (string ChildDir in Result)
{
TreeNode ChildNode = new TreeNode(ChildDir);
Console.WriteLine("Start Childnode :" + ChildNode.Text);
Node.Nodes.Add(ChildNode);
recureseChild(ChildNode, root + "/" + ChildDir + "/");
}
I think its not loading the directories in the right way
I get this exception each in the debug
Failure in Recurse Child : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
&
A first chance exception of type 'System.Net.WebException' occurred in System.dll
Please help ??
Any Ideas ...:icon_cool: