Flow

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A directory tree, recursive loading

 1 string path=@"D:\A";
 2 
 3 public Form1()
 4 
 5 {
 6 
 7 LoadTree(path,null);
 8 
 9 }
10 
11 public void LoadTree(string path,TreeNode node)
12 
13 {
14 
15 //Specifies all the subdirectories under the path stored in arrays
16 
17 string[] dirs=Directory.GetDirectories(path);
18 
19 foreach(string dir in dirs)
20 
21 {
22 
23    TreeNode node1=new TreeNode(Path.GetFileName(dir));
24 
25    if(node==null)
26    {
27 
28    tvData.Nodes.Add(node1);//Added to the root directory
29 
30    }
31 
32    else
33 
34    {
35 
36     node.Nodes.Add(node1);
37 
38    }
39 
40    //If dir and subdirectories
41 
42    if(Directory.GetDirectories(dir).Length>0)
43 
44    {
45 
46        LoadTree(dir,node1);
47 
48    }
49 
50 }
51 
52 }

Two, a file stream to read or write operation


A, serialization / deserialization

 1 class Person
 2 
 3 {
 4 
 5 public int Age{get;set;}
 6 
 7 public string Name{get;set;}
 8 
 9 }
10 
11 class Program
12 
13 {
14 
15   Person p=new Person();
16 
17   if(File.Exists("1.txt"))
18 
19   {
20 
21     string[] lins=File.ReadAllLines("1.txt",Encoding.Default);
22 
23     p.Age=int.Parse(lines[0]);
24 
25     p.Name=lines[1];
26 
27   }
28 
29   else
30 
31   {
32 
33     p.Age=17;
34 
35     p.Name="jack";
36 
37     File.WriteAllLines("1.txt",new string[]{p.Age.ToString(),p.Name});//Write the current assembly in the bin folder
38 
39   }
40 
41 Console.WriteLine(p.Age+":"+p.Name);
42 
43 Consoles.ReadKey();
44 
45 }

If the Person is how to do the 100 field? This is the time to use serialization (the state of an object persistence to a piece of equipment)

 1 [Serializable]//Said Person can be serialized
 2 
 3 class Person
 4 
 5 {
 6 
 7 public int Age{get;set;}
 8 
 9 public string Name{get;set;}
10 
11 }
12 
13 class Program
14 
15 {
16 
17 static void Main(string[] agrs)
18 
19 {
20 
21 Person p=new Person(){Age=12,Name="rose"};
22 
23 //Serialization
24 
25 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
26 
27 using(FileStream fs=new FileStream("se.bin",FilreMode.Create))
28 
29 {
30 
31  bf.Serialize(fs,p);//The state of preservation of P to se.bin, with binary serialization, rather than a text document
32 
33 }
34 
35 //Deserialization
36 
37 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
38 
39 using(FireStream fs=new FileStream("se.bin",File.Open))
40 
41 {
42 
43   object obj=bf.Deserialize(fs);//Deserialization
44 
45   Person p=obj as Person;
46 
47   Console.WriteLine(p.Name+":"+p.Age);
48 
49 }
50 
51 }
52 
53 }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Claudia at November 14, 2013 - 3:50 PM