C # file/folder related methods

Source: Internet
Author: User

 

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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.