forked from pmengal/MailSystem.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderUtil.cs
More file actions
109 lines (95 loc) · 3.08 KB
/
FolderUtil.cs
File metadata and controls
109 lines (95 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace ActiveUp.MailSystem.DesktopClient.Folder
{
/// <summary>
/// This class contains utilities methods for folders.
/// </summary>
public class FolderUtil
{
#region Save (saveTree, saveNode)
/// <summary>
/// Save the TreeView content.
/// </summary>
/// <param name="tree">The tree view.</param>
/// <returns>Error code as int.</returns>
public static int SaveTree(TreeView tree)
{
string filename = GetFolderFile();
ArrayList al = new ArrayList();
foreach (TreeNode tn in tree.Nodes)
{
al.Add(tn);
}
Stream file = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
try
{
bf.Serialize(file, al);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("Serialization failed : {0}", e.Message);
return -1;
}
file.Close();
return 0;
}
#endregion
#region Load (loadTree, searchNode)
/// <summary>
/// Load the TreeView content.
/// </summary>
/// <param name="tree">The tree view.</param>
/// <returns>Error code as int.</returns>
public static int LoadTree(TreeView tree)
{
string filename = GetFolderFile();
if (File.Exists(filename))
{
tree.Nodes.Clear();
Stream file = File.Open(filename, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
object obj = null;
try
{
obj = bf.Deserialize(file);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("De-Serialization failed : {0}", e.Message);
return -1;
}
file.Close();
ArrayList nodeList = obj as ArrayList;
foreach (TreeNode node in nodeList)
{
tree.Nodes.Add(node);
}
return 0;
}
else return -2;
}
#endregion
/// <summary>
/// Method for get the folder file.
/// </summary>
/// <returns>The string file path.</returns>
private static string GetFolderFile()
{
// verify if the messages directory exist, if not create it.
string directory = Constants.Messages;
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string path = Path.Combine(directory, "tree.folders");
return path;
}
}
}