C # create and use folders and files

Source: Internet
Author: User

In C #, directory classes include: directoryinfo and directory. The Directory class only provides static methods and cannot be instantiated.

File classes include: filestream, file, and fileinfo.

Here only two simple applications are described:

(1) create a file in the current directory

In fact, the method is very simple. You only need to use the current directory name + "\" + file name to obtain the path name of the file.

The Code is as follows:

// Obtain the current directory directoryinfo mydi = new directoryinfo (". "); console. writeline (mydi. fullname); // the current directory name string filepath = mydi. fullname + @ "\ 412947095.db"; // create the file filestream myfs = new filestream (filepath, filemode. create, fileaccess. write); console. writeline (myfs. name); // close the file myfs. close ();

(2) create a subdirectory under the current directory

There are two methods (I know)

1> bind the directory before creating it. Because the directory constructor does not check whether the directory exists, you can bind it to the directory and then create it using the CREATE () function.

Note: The current directory name + "\" + sub-directory name is the sub-directory path name, which is similar to creating files in the current directory.

The Code is as follows:

            DirectoryInfo myDirectoryInfo = new DirectoryInfo("."); ;            DirectoryInfo newDirectoryInfo = new DirectoryInfo(myDirectoryInfo.FullName + @"\412947095");            newDirectoryInfo.Create();

2> first obtain the current directory, and then use the createsubdirectory () method of the directoryinfo class to create a subdirectory.

The Code is as follows:

            DirectoryInfo directInfo = new DirectoryInfo(".");            string usrName = "412947095";            DirectoryInfo myFileDirect = directInfo.CreateSubdirectory(usrName);

 

 

 

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.