C # File 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 indirectory. 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 indirectory. 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 = newstreamwriter (server. mappath ("A // a.txt "));
Streamwrite. writeline ("wooden house ");
Streamwrite. writeline ("http://www.mzwu.com /");
Streamwrite. Write ("2008-04-13 ");
Streamwrite. Close ();

Streamreader = newstreamreader (server. mappath ("A // a.txt "));
Response. Write (streamreader. Readline ());
Response. Write (streamreader. readtoend ());
Streamreader. Close ();
}

 

 

 

Get the file version information:

Fileversioninfo myfileversioninfo1 = fileversioninfo. getversioninfo ("D: // test. dll ");
Textbox1.text = "version:" + myfileversioninfo1.fileversion;

Modify file attributes and delete read-only files:

In the following example, you want to copy the E:/test.txt file to D:/tmp/test.txt, but D:/tmp/test.txt already exists.

// File. Copy (sourcefile, destinationfile, true); used to copy an object
// When destinationfile already exists, you cannot copy file file1 to the target file,
// Delete the destination file first. The file. Delete () method cannot delete the read-only file,
// Therefore, if the file attribute is read-only (the attributes attribute contains "readonly "),
// First reset the file property to normal and then delete the file:
String file1 = "E: // test.txt ";
String destinationfile = "D: // TMP // test.txt ";
If (file. exists (destinationfile ))
{
Fileinfo Fi = new fileinfo (destinationfile );
If (Fi. Attributes. tostring (). indexof ("readonly ")! =-1)
Fi. Attributes = fileattributes. normal;
File. Delete (destinationfile );
}
File. Copy (file1, destinationfile, true );

Determine whether the file exists: file. exists (stringfilepath)

Determine whether the directory exists: directory. exists ("D: // lastestversion ")

Read files by row:

Int filecount = 0;
// Open the file just specified such that no one else can useit.
Streamreader sr = new streamreader (textbox1.text. Trim ());
While (Sr. Peek ()>-1) // streamreader. Peek () returns the next available character, but does not use it
{
Listbox1.items. Add (Sr. Readline ());
Filecount ++;
}
Sr. Close ();

Write files by row:
Streamwriter Sw = new streamwriter ("D: // result.txt ");
For (INT I = 0; I <10; I ++)
{
Sw. writeline ("this is the" + I. tostring () + "row data ");
}

C # File Operations

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.