First i have this method that i'm using to get the content of my ftp server:
private int total_dirs;
private int searched_until_now_dirs;
private int max_percentage;
private TreeNode directories_real_time;
private string SummaryText;
private TreeNode CreateDirectoryNode(string path, string name , int recursive_levl )
{
var directoryNode = new TreeNode(name);
var directoryListing = GetDirectoryListing(path);
var directories = directoryListing.Where(d => d.IsDirectory);
var files = directoryListing.Where(d => !d.IsDirectory);
total_dirs += directories.Count<FTPListDetail>();
searched_until_now_dirs++;
int percentage = 0;
foreach (var dir in directories)
{
directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name, recursive_levl+1));
if (recursive_levl == 1)
{
TreeNode temp_tn = (TreeNode)directoryNode.Clone();
this.BeginInvoke(new MethodInvoker( delegate
{
UpdateList(temp_tn);
}));
}
percentage = (searched_until_now_dirs * 100) / total_dirs;
if (percentage > max_percentage)
{
SummaryText = String.Format("Searched dirs {0} / Total dirs {1}", searched_until_now_dirs, total_dirs);
max_percentage = percentage;
backgroundWorker1.ReportProgress(percentage, SummaryText);
}
}
percentage = (searched_until_now_dirs * 100) / total_dirs;
if (percentage > max_percentage)
{
SummaryText = String.Format("Searched dirs {0} / Total dirs {1}", searched_until_now_dirs, total_dirs);
max_percentage = percentage;
backgroundWorker1.ReportProgress(percentage, SummaryText);
}
foreach (var file in files)
{
TreeNode file_tree_node = new TreeNode(file.Name);
file_tree_node.Tag = "file";
directoryNode.Nodes.Add(file_tree_node);
numberOfFiles.Add(file.FullPath);
}
return directoryNode;
}
The line here that i'm using the Tag property is:
file_tree_node.Tag = "file";
Then i have a small method that i'm upading/building the treeView structure in real time:
DateTime last_update;
private void UpdateList(TreeNode tn_rt)
{
TimeSpan ts = DateTime.Now - last_update;
if (ts.TotalMilliseconds > 200)
{
last_update = DateTime.Now;
treeViewMS1.BeginUpdate();
treeViewMS1.Nodes.Clear();
treeViewMS1.Nodes.Add(tn_rt);
TreeViewXmlPopulation.SerializeTreeView(treeViewMS1, @"c:\XmlFile\Original.xml");
ExpandToLevel(treeViewMS1.Nodes, 1);
treeViewMS1.EndUpdate();
}
}
I'm also building/creating the xml file that will hold the treeView structure in real time. Original.xml
And this is the class of how i'm creating building the xml file treeView structure:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows.Forms;
namespace FTP_ProgressBar
{
class TreeViewXmlPopulation
{
// Xml tag for node, e.g. 'node' in case of <node></node>
private const string XmlNodeTag = "node";
// Xml attributes for node e.g. <node text="Asia" tag=""
// imageindex="1"></node>
private const string XmlNodeTextAtt = "text";
private const string XmlNodeTagAtt = "tag";
private const string XmlNodeImageIndexAtt = "imageindex";
public static void DeserializeTreeView(TreeView treeView, string fileName)
{
XmlTextReader reader = null;
try
{
// disabling re-drawing of treeview till all nodes are added
treeView.BeginUpdate();
reader = new XmlTextReader(fileName);
TreeNode parentNode = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == XmlNodeTag)
{
TreeNode newNode = new TreeNode();
bool isEmptyElement = reader.IsEmptyElement;
// loading node attributes
int attributeCount = reader.AttributeCount;
if (attributeCount > 0)
{
for (int i = 0; i < attributeCount; i++)
{
reader.MoveToAttribute(i);
SetAttributeValue(newNode,
reader.Name, reader.Value);
}
}
// add new node to Parent Node or TreeView
if (parentNode != null)
parentNode.Nodes.Add(newNode);
else
treeView.Nodes.Add(newNode);
// making current node 'ParentNode' if its not empty
if (!isEmptyElement)
{
parentNode = newNode;
}
}
}
// moving up to in TreeView if end tag is encountered
else if (reader.NodeType == XmlNodeType.EndElement)
{
if (reader.Name == XmlNodeTag)
{
parentNode = parentNode.Parent;
}
}
else if (reader.NodeType == XmlNodeType.XmlDeclaration)
{
//Ignore Xml Declaration
}
else if (reader.NodeType == XmlNodeType.None)
{
return;
}
else if (reader.NodeType == XmlNodeType.Text)
{
parentNode.Nodes.Add(reader.Value);
}
}
}
finally
{
// enabling redrawing of treeview after all nodes are added
treeView.EndUpdate();
reader.Close();
}
}
/// <span class="code-SummaryComment"><summary>
/// Used by Deserialize method for setting properties of
/// TreeNode from xml node attributes
/// <span class="code-SummaryComment"></summary>
private static void SetAttributeValue(TreeNode node,
string propertyName, string value)
{
if (propertyName == XmlNodeTextAtt)
{
node.Text = value;
}
else if (propertyName == XmlNodeImageIndexAtt)
{
node.ImageIndex = int.Parse(value);
}
else if (propertyName == XmlNodeTagAtt)
{
node.Tag = value;
}
}
public static void SerializeTreeView(TreeView treeView, string fileName)
{
XmlTextWriter textWriter = new XmlTextWriter(fileName,
System.Text.Encoding.ASCII);
// writing the xml declaration tag
textWriter.WriteStartDocument();
//textWriter.WriteRaw("\r\n");
// writing the main tag that encloses all node tags
textWriter.WriteStartElement("TreeView");
// save the nodes, recursive method
SaveNodes(treeView.Nodes, textWriter);
textWriter.WriteEndElement();
textWriter.Close();
}
private static void SaveNodes(TreeNodeCollection nodesCollection,
XmlTextWriter textWriter)
{
for (int i = 0; i < nodesCollection.Count; i++)
{
TreeNode node = nodesCollection[i];
textWriter.WriteStartElement(XmlNodeTag);
textWriter.WriteAttributeString(XmlNodeTextAtt,
node.Text);
textWriter.WriteAttributeString(
XmlNodeImageIndexAtt, node.ImageIndex.ToString());
if (node.Tag != null)
textWriter.WriteAttributeString(XmlNodeTagAtt,
node.Tag.ToString());
// add other node properties to serialize here
if (node.Nodes.Count > 0)
{
SaveNodes(node.Nodes, textWriter);
}
textWriter.WriteEndElement();
}
}
}
}
And this is how i'm using the xml file once i created it when i'm running the program each time later i'm reading the xml file in the constructor and add it to the treeView:
if (File.Exists(@"c:\XmlFile\Original.xml"))
{
TreeViewXmlPopulation.DeserializeTreeView(treeViewMS1, @"c:\XmlFile\Original.xml");
}
Last thing is example of how i'm using the Tag property to identify what is "file" and what is "directory":
if (treeViewMS1.SelectedNode.Tag != null)
{
string s = (string)treeViewMS1.SelectedNode.Tag;
if (s == "file")
{
file = false;
DeleteFile(treeViewMS1.SelectedNode.FullPath, file);
}
}
else
{
RemoveDirectoriesRecursive(treeViewMS1.SelectedNode, treeViewMS1.SelectedNode.FullPath);
}
I'm not sure if i needed to add all the methods and code to here but it's all connected to my question:
The problem is when i'm running the program over again the Tag property is null i can't identify what is "file" and what is "directory". Each time i'm running the program again i need to get the ftp content from the server to use the Tag property.
The question is how can i add to the xml file when i build it in real time also the Tag property values/s so when i'm running the program over again i will know in the treeView by using the Tag property what is "file" and what is "directory" ?
No comments:
Post a Comment