C
Chocolade1972
Guest
The first thing im doing is to get from my ftp server all the ftp content information and i tag it so i can identify later if its a file or a directory:
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 im using to Tag is:
file_tree_node.Tag = "file";
So i know what is "file" then i make a simple check if the Tag is not null then i know its a "file" if its null then its directory.
For example this is how im checking if its file or directory after getting all the content from my ftp server:
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 also update in real time when getting the content of the ftp server xml file on my hard disk with the treeView structure information so when im running the program each time it will load the treeView structure with all directories and files.
This is the class of the xml file:
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;
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 im using the class this method im calling it inside the CreateDirectoryNode and im updating the treeView in real time when getting the ftp content from the server i build 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();
}
}
And when im running the program again in the constructor im doing:
if (File.Exists(@"c:\XmlFile\Original.xml"))
{
TreeViewXmlPopulation.DeserializeTreeView(treeViewMS1, @"c:\XmlFile\Original.xml");
}
My question is how can i update the xml file in real time like im doing now but also with the Tag property so next time i will run the program and will not get the content from the ftp i will know in the treeView what is file and what is directory.
The problem is that now if i will run the program the Tag property is null. I must get the ftp content from the server each time.
But i want that withoutout getting the ftp content from server to Tag each file as file in the treeView structure.
So what i need is somehow where i;m Tagging "file" or maybe when updating the treeView also to add something to the xml file so when i will run the progrma again and read back the xml file it will also Tag the files in the treeView.
Continue reading...
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 im using to Tag is:
file_tree_node.Tag = "file";
So i know what is "file" then i make a simple check if the Tag is not null then i know its a "file" if its null then its directory.
For example this is how im checking if its file or directory after getting all the content from my ftp server:
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 also update in real time when getting the content of the ftp server xml file on my hard disk with the treeView structure information so when im running the program each time it will load the treeView structure with all directories and files.
This is the class of the xml file:
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;
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 im using the class this method im calling it inside the CreateDirectoryNode and im updating the treeView in real time when getting the ftp content from the server i build 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();
}
}
And when im running the program again in the constructor im doing:
if (File.Exists(@"c:\XmlFile\Original.xml"))
{
TreeViewXmlPopulation.DeserializeTreeView(treeViewMS1, @"c:\XmlFile\Original.xml");
}
My question is how can i update the xml file in real time like im doing now but also with the Tag property so next time i will run the program and will not get the content from the ftp i will know in the treeView what is file and what is directory.
The problem is that now if i will run the program the Tag property is null. I must get the ftp content from the server each time.
But i want that withoutout getting the ftp content from server to Tag each file as file in the treeView structure.
So what i need is somehow where i;m Tagging "file" or maybe when updating the treeView also to add something to the xml file so when i will run the progrma again and read back the xml file it will also Tag the files in the treeView.
Continue reading...