1. check whether a folder exists.
1 // <summary> whether the folder exists
2 /// </Summary>
3 /// <Param name = "dir"> </param>
4 /// <returns> </returns>
5 public static Boolean isfolderexist (string DIR)
6 {
7 if (system. Io. Directory. exists (DIR ))
8 return true;
9 return false;
10}
2. Check whether the folder is empty.
1 // <summary> whether the folder is empty
2 /// </Summary>
3 /// <Param name = "dir"> </param>
4 /// <returns> </returns>
5 public static Boolean isfolderempty (string DIR)
6 {
7 if (! Isfolderexist (DIR) return false;
8
9 system. Io. directoryinfo di = new directoryinfo (DIR );
10 if (Di. getfiles (). Length + DI. getdirectories (). Length = 0)
11 return true;
12 Return false;
13}
3. Create a folder
1 // <summary> Create a folder
2 /// </Summary>
3 /// <Param name = "dir"> </param>
4 /// <returns> </returns>
5 public static void createfolder (string DIR)
6 {
7 if (! Isfolderexist (DIR ))
8 system. Io. Directory. createdirectory (DIR );
9}
4. Determine whether the file name is legal
1 // <summary> determine whether the file name/folder name is legal
2 /// </Summary>
3 /// <Param name = "FILENAME"> </param>
4 /// <returns> </returns>
5 public static bool isvalidfilename (string filename)
6 {
7 bool isvalid = true;
8 string errchar = "\\/:*? \ "<> | ";//
9 If (string. isnullorempty (filename ))
10 {
11 isvalid = false;
12}
13 else
14 {
15 For (INT I = 0; I <errchar. length; I ++)
16 {
17 if (filename. Contains (errchar [I]. tostring ()))
18 {
19 isvalid = false;
20 break;
21}
22}
23}
24 return isvalid;
25}
5. delete a folder
1 // <summary> delete a folder and its content
2 /// </Summary>
3 /// <Param name = "from"> original folder address </param>
4 public static void deletefolder (string from)
5 {
6 if (! Directory. exists (from ))
7 return;
8
9 foreach (string sub in directory. getdirectories (from ))
10 deletefolder (sub + "\\");
11
12 foreach (string file in directory. getfiles (from ))
13 file. Delete (File );
14
15}
6. Copy a folder
1 // <summary> copy the folder and its content
2 /// </Summary>
3 /// <Param name = "from"> original folder address </param>
4 /// <Param name = "to"> Target folder address </param>
5 public static void copyfolder (string from, string)
6 {
7 if (! Directory. exists ())
8 directory. createdirectory ();
9
10 foreach (string sub in directory. getdirectories (from ))
11 copyfolder (sub + "\", to + "\" + system. Io. Path. getfilename (sub ));
12
13 foreach (string file in directory. getfiles (from ))
14 file. Copy (file, to + "\" + system. Io. Path. getfilename (file), true );
15
16}
7. Open a specified folder
1 // <summary> open the specified folder
2 /// </Summary>
3 /// <Param name = "dir"> </param>
4 public static void openfolderinwindow (string DIR)
5 {
6 system. Diagnostics. process. Start ("assumer.exe", DIR );
7}