Operations of the C # directory Class

Source: Internet
Author: User

The directory class is located in the System.IO namespace. The directory class provides static methods for creating movement and enumeration operations in directories and subdirectories. In addition, you can access and manipulate a wide variety of directory properties, such as the time of creation or last modification, and the Windows access control list.

System.IO.Directory class and System.directoryinfo class

It mainly provides various operations about the directory, which need to refer to the System.IO namespace. The main properties and methods are described below through a program instance.

Method/Step
  1. 1

    Directory Creation Method: Directory.CreateDirectory

    The method is declared as follows:

    public static DirectoryInfo CreateDirectory (string path);

    The following code demonstrates creating a directory named NewDirectory under the C:\tempuploads folder.

    private void Makedirectory ()

    {

    Directory.CreateDirectory (@ "C:\tempuploads\NewDirectoty");

    }

  2. 2

    Directory property setting Method: Directoryinfo.atttributes

    The following code sets the C:\tempuploads\NewDirectory directory to read-only and hidden. As with the file properties, the directory properties are also set using FileAttributes.

    private void Setdirectory ()

    {

    DirectoryInfo newdirinfo = new DirectoryInfo (@ "C:\tempuploads\NewDirectoty");

    Newdirinfo.atttributes = fileattributes.readonly| Fileattributes.hidden;

    }

  3. 3

    Directory deletion method: Directory.delete

    The method is declared as follows:

    public static void Delete (string path,bool recursive);

    The following code can remove the C:\tempuploads\BackUp directory. The second parameter of the Delete method is type bool, which can decide whether to delete a non-empty directory. If the parameter value is true, the entire directory is deleted, even if there are files or subdirectories under that directory, or False if the directory is empty.

    private void DeleteDirectory ()

    {

    Directory.delete (@ "C:\tempuploads\BackUp", true);

    }

  4. 4

    Directory Move method: Directory.move

    The method is declared as follows:

    public static void Move (String sourcedirname,string destdirname);

    The following code moves the directory c:\tempuploads\NewDirectory to C:\tempuploads\BackUp.

    private void MoveDirectory ()

    {

    File.move (@ "c:\tempuploads\NewDirectory", @ "c:\tempuploads\BackUp");

    }

  5. 5

    Get all subdirectories in current directory method: Directory.getdirectories

    The method is declared as follows:

    public static string[] GetDirectories (string path);

    The following code reads all subdirectories under the C:\tempuploads\ directory and stores them in a string array.

    private void Getdirectory ()

    {

    string [] Directorys;

    Directorys = Directory. GetDirectories (@ "c:\tempuploads");

    }

  6. 6

    Get all file methods in current directory: Directory.GetFiles

    The method is declared as follows:

    public static string[] GetFiles (string path;);

    The following code reads all the files in the C:\tempuploads\ directory and stores them in a string array.

    private void GetFile ()

    {

    string [] Files;

    Files = Directory. GetFiles (@ "c:\tempuploads");

    }

  7. 7

    Determine if the directory exists method: Directory.exist

    The method is declared as follows:

    public static bool Exists

    (

    string path;

    );

    The following code determines whether the C:\tempuploads\NewDirectory directory exists. If present, get the subdirectories and files under the directory, then move them, and finally delete the moved directory. If it does not exist, the directory is created first and then the Directory property is set to read-only, hidden

    if (File.exists (@ "c:\tempuploads\NewDirectory"))//Determine if the directory exists

    {

    Getdirectory (); Get subdirectories

    GetFile (); Get file

    MoveDirectory (); Move Directory

    DeleteDirectory (); Delete Directory

    }

    Else

    {

    Makedirectory (); Build Directory

    Setdirectory (); Set directory Properties

    }

    Attention:

    The path has 3 ways, relative path under current directory, relative path of current work disk, absolute path. Take C:\Tmp\Book as an example (assuming the current working directory is C:\TMP). "Book", "\tmp\book", "C:\Tmp\Book" all represent C:\Tmp\Book.

    Also, in C #, "\" is a special character and you need to use "\ \" to represent it. Because of this inconvenient wording, the C # language provides a simplified @. You can use "\" directly before the string by adding @. So the above path should be represented as "book" in C #, @ "\tmp\book", @ "C:\Tmp\Book".

Operations of the C # directory Class

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.