So from now I will add some convenient functions. I added a favorites function today.
A tree view for favorites is made by TreeView that System.Windows.Forms provide.
File format for save favorites items is XML and to save XML I used a XMLWriter and to load XML I used a XMLReader.
XMLReader and XMLWriter was very useful for XML control.
1. Update lists
- Added add favorites,
- Added delete favorites.
- Seperated ftp address and port and path.
2. GFtp v1.4 demo
3. Code snippets
- Extend method defines
- The extend method in C# is very useful. To make customized control, you don't have to make new custom control class what is Inherited existed control.
- TreeView control Helper for favorites tree view
- To make extend method, Just remember just two things that static and this.
- Following is extend method for TreeView control for favorites.
static public class FavoritesTreeViewHelper
{
// Save favorites items from favorites.xml file.
static public bool SaveFavoritesItems(this TreeView treeView, Favorites favorites)
{
...
}
// Load favorites items from favorites.xml file.
static public bool LoadFavoritesItems(this TreeView treeView, Favorites favorites)
{
...
}
// Refresh favorites items
static public void RefreshFavoritesItems(this TreeView treeView, Favorites favorites)
{
...
}
// Get selected favorites item
static public FavoritesItem GetSelectedFavoritesItem(this TreeView treeView, Favorites favorites)
{
...
}
// Get selected favorites item group name
static public string GetSelectedFavoritesItemGroupName(this TreeView treeView, Favorites favorites)
{
...
}
// Delete selected favorites item
static public bool DeleteSelectedFavoritesItem(this TreeView treeView, Favorites favorites)
{
...
}
}
- Following is way to call extend method.
- You can call it like a member method.
favoritesTreeView.SaveFavoritesItems(_favorites);
favoritesTreeView.DeleteSelectedFavoritesItem(_favorites);
favoritesTreeView.RefreshFavoritesItems(_favorites);
favoritesTreeView.LoadFavoritesItems(_favorites);
- You can make a extend method that has return value.
string groupName = favoritesTreeView.GetSelectedFavoritesItemGroupName(_favorites);
- XML Reader
- Following is GFtp's favorites structure.
- Root elementis is <Favorites> and <Favorites> has some <Group> elements.
And <Group> element has some <Item> element.
<Item> element has attributes that are name, address, port, path, id, password.
- The extend method for loading favorites.xml file.
I called just XmlReader.Read() and XmlReader.GetAttribute(...) method.
It's very useful for simple and small xml file. but For big xml file, you need more method in XmlReader, or you can use other class for xml like XmlDocument.
// Load favorites items from favorites.xml file.
static public bool LoadFavoritesItems(this TreeView treeView, Favorites favorites)
{
XmlReaderSettings setting = new XmlReaderSettings();
setting.IgnoreWhitespace = true; // Remove all unnecessary white spaces in the xml file.
setting.ConformanceLevel = ConformanceLevel.Fragment;
try
{
List<FavoritesItem> items = null;
using (XmlReader reader = XmlReader.Create("favorites.xml", setting))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "Group")
{
items = favorites.GetGroup(reader.GetAttribute("name"));
continue;
}
if (reader.Name == "Item")
{
if(items == null)
{
continue;
}
FavoritesItem item = new FavoritesItem();
item.Name = reader.GetAttribute("name");
item.Address = reader.GetAttribute("address");
item.Port = reader.GetAttribute("port");
item.Path = reader.GetAttribute("path");
item.ID = reader.GetAttribute("id");
item.Password = reader.GetAttribute("password");
items.Add(item);
}
}
}
}
}
catch
{
MessageBox.Show("Not found favorites.xml");
}
return true;
}
- Next is extend method to save savorites items to xml file.
Because it is very simple and small xml file, I used the XmlWriter.
// Save favorites items from favorites.xml file.
static public bool SaveFavoritesItems(this TreeView treeView, Favorites favorites)
{
XmlWriterSettings setting = new XmlWriterSettings();
setting.Indent = true;
try
{
List<FavoritesItem> items = null;
using (XmlWriter writer = XmlWriter.Create("favorites.xml", setting))
{
writer.WriteStartElement("Favorites");
foreach(var group in favorites._group)
{
string groupName = group.Key;
writer.WriteStartElement("Group");
writer.WriteAttributeString("name", groupName);
foreach(var item in group.Value)
{
writer.WriteStartElement("Item");
writer.WriteAttributeString("name", item.Name);
writer.WriteAttributeString("address", item.Address);
writer.WriteAttributeString("port", item.Port);
writer.WriteAttributeString("path", item.Path);
writer.WriteAttributeString("id", item.ID);
writer.WriteAttributeString("password", item.Password);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}
catch
{
MessageBox.Show("Not found favorites.xml");
}
return true;
}
- Logic to add favorites and to delete favorites.
- To add favorites :
- Clicks add button.
- New item is added to favorites data class.
- The favorites data class is saved to xml file.
- Favorites tree view control is refreshed using favorites data class.
- To delete favorites.
- Clicks del button.
- Finds favorites item in the favorites data class by selected item in favorites tree view cocntrol.
- Deletes favorites item in the favorites data class.
- The favorites data class is saved to xml file.
- Favorites tree view control is refreshed using favorites data class.
4. Next
- Now GFtp is became so big and complex.
And GFtp is done almost basic function. So I will update GFtp for powerful and stability.
I will try speed up translate using multi threading. By the way I don't know what I can do it.
댓글 없음:
댓글 쓰기