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);