Ah man , ive been adjusting the code so much ...
I am trying to make a treeview that display's all the directories and files on theserver (SO FAR SO GOOD) ...
Here's my code to retrieve Directories and files
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/"));
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();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
public string[] GetChildFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + txtftpServerName.Text + "/" + treeView1.SelectedNode.Text + "/"));
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();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
This works fine indevidually , but when i try to combine the two:
private void btnDoIt_Click(object sender, EventArgs e)
{
string[] Parent_Name = GetFileList();
string[] Child_Name = GetChildFileList();
treeView1.Nodes.Clear();
if (Parent_Name != null)
{
/* Parent Node */
foreach (string filename in Parent_Name)
{
TreeNode ParentNode = new TreeNode(filename);
treeView1.Nodes.Add(ParentNode);
if (Parent_Name != null)
{
foreach (string filename2 in Child_Name)
{
TreeNode ChildNode = new TreeNode(filename2);
ParentNode.Nodes.Add(ChildNode);
}
}
}
}
}
i get an error stating that "Object reference not set to an instance of an object." (LENS ON [foreach (string filename2 in Child_Name)] )
Can anyone maybe give me a clue ?? i dont even know why its complaining because it aught to work...
Happy coding