C # file and folder operations

Source: Internet
Author: User
// 1. Create, move, and delete folders ---------
// Create a folder
Directory. createdirectory (server. mappath (""));
Directory. createdirectory (server. mappath ("B "));
Directory. createdirectory (server. mappath ("C "));
// Move B to
Directory. Move (server. mappath ("B"), server. mappath ("A \ B "));
// Delete C
Directory. Delete (server. mappath ("C "));

// 2. Create, copy, move, and delete a file ---------
// Create a file
// When file. Create is used to create, copy, move, or delete a file, the system prompts that the file is being used by another process, so the process cannot access the file.
// Use filestream to retrieve the system. Io. filestream returned by file. Create and disable it.
Filestream FS;
FS = file. Create (server. mappath ("a.txt "));
FS. Close ();
FS = file. Create (server. mappath ("B .txt "));
FS. Close ();
FS = file. Create (server. mappath ("c.txt "));
FS. Close ();
// Copy an object
File. Copy (server. mappath ("a.txt"), server. mappath ("A \ a.txt "));
// Move the file
File. Move (server. mappath ("B .txt"), server. mappath ("A \ B .txt "));
File. Move (server. mappath ("c.txt"), server. mappath ("A \ c.txt "));
// Delete an object
File. Delete (server. mappath ("a.txt "));

// 3. traverse the files and subfolders in the folder and display their attributes ---------
If (directory. exists (server. mappath ("")))
{
// All subfolders
Foreach (string item in directory. getdirectories (server. mappath ("")))
{
Response. Write ("<B> Folder:" + item + "</B> <br/> ");
Directoryinfo = new directoryinfo (item );
Response. Write ("name:" + directoryinfo. Name + "<br/> ");
Response. Write ("Path:" + directoryinfo. fullname + "<br/> ");
Response. Write ("creation time:" + directoryinfo. creationtime + "<br/> ");
Response. Write ("last access time:" + directoryinfo. lastaccesstime + "<br/> ");
Response. Write ("last modification time:" + directoryinfo. lastwritetime + "<br/> ");
Response. Write ("parent folder:" + directoryinfo. Parent + "<br/> ");
Response. Write ("root directory:" + directoryinfo. Root + "<br/> ");
Response. Write ("<br/> ");
}

// All sub-Files
Foreach (string item in directory. getfiles (server. mappath ("")))
{
Response. Write ("<B> file:" + item + "</B> <br/> ");
Fileinfo = new fileinfo (item );
Response. Write ("name:" + fileinfo. Name + "<br/> ");
Response. Write ("Extension:" + fileinfo. Extension + "<br/> ");
Response. Write ("Path:" + fileinfo. fullname + "<br/> ");
Response. Write ("Size:" + fileinfo. Length + "<br/> ");
Response. Write ("creation time:" + fileinfo. creationtime + "<br/> ");
Response. Write ("last access time:" + fileinfo. lastaccesstime + "<br/> ");
Response. Write ("last modification time:" + fileinfo. lastwritetime + "<br/> ");
Response. Write ("folder:" + fileinfo. directoryname + "<br/> ");
Response. Write ("file attributes:" + fileinfo. Attributes + "<br/> ");
Response. Write ("<br/> ");
}
}
// 4. file read/write ---------
If (file. exists (server. mappath ("A \ a.txt ")))
{
Streamwriter streamwrite = new streamwriter (server. mappath ("A \ a.txt "));
Streamwrite. writeline ("wooden house ");
Streamwrite. writeline ("http://www.mzwu.com /");
Streamwrite. Write ("2008-04-13 ");
Streamwrite. Close ();

Streamreader = new streamreader (server. mappath ("A \ a.txt "));
Response. Write (streamreader. Readline ());
Response. Write (streamreader. readtoend ());
Streamreader. Close ();
}
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.