Take a TXT file as an example to learn the basic operations of ASP. NET files. The following code should be clear:
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; // introduce system. iousing system. io; namespace filetry {public partial class webform1: system. web. UI. page {protected void page_load (Object sender, eventargs e) {// The text file does not mean that the extension is. TXT files, as long as they are stored in ASCII files, such as: .aspx0000.htm0000.css ,. INI and so on. Renameex () ;}//< summary> /// read example /// </Summary> private void readex () {// source file, must exist. String path1 = server. mappath ("goo1.txt"); // returns the physical file path corresponding to the specified virtual path on the Web server if (file. exists (path1) // determines whether the specified file exists. If yes, true is returned. If no, false is returned. {// file. an exception occurs when the opentext target does not exist. Using (streamreader reader = // implements a system. io. streamreader reads the character file from the byte stream with a specific encoding. opentext (path1) // open an existing UTF-8 encoding file to read {string STR = reader. readline (); // read a line of characters from the current stream and return the data as a string to reader. close (); // close system. io. streamreader object and basic stream, and release all system resources associated with the reader }}/// <summary> /// write example /// </Summary> private void writeex () {// source file, must exist. String path1 = server. mappath ("goo1.txt"); If (file. exists (path1) {// file. If the createtext target exists, it will overwrite it. This is equivalent to deleting using (streamwriter writer = file. createtext (path1) // create or open a file to write the UTF-8-encoded text {writer. writeline ("Write a line of content"); // write the string followed by the line terminator to the text stream writer. close () ;}}/// <summary> // append example /// </Summary> private void elseex () {// source file, must exist. String path1 = server. mappath ("goo1.txt"); If (file. exists (path1) {using (streamwriter writer = file. appendtext (path1) // create a system. io. streamreader, which traces UTF-8-encoded text to existing File, the original content {writer. writeline ("append a line of content"); writer. close () ;}}/// <summary> // Delete example (not limited to file type) /// </Summary> private void deleteex () {// source file, which must exist. String path1 = server. mappath ("goo1.txt"); If (file. exists (path1) {// file. if the delete object does not exist, skip this step. File. delete (path1) ;}//< summary> /// copy the file /// </Summary> private void copyex () {// source file, must exist. String path1 = server. mappath ("goo.txt"); // target file string path2 = server. mappath ("goo1.txt"); // file. copy (server. mappath ("goo.txt"), server. mappath ("goo1.txt"); // copy the existing file to the new file. The file with the same name can be overwritten. /** The first parameter indicates the source file path, * the second parameter indicates the target file path, and * The third parameter indicates whether to overwrite the target file when it exists. The default value is false. * If the value is false, an exception occurs when the target file exists, rather than skipping the copy. -- The exception is that the file already exists. */If (file. exists (path1 )&&! File. exists (path2) {file. copy (path1, path2, true) ;}/// <summary >/// move the file /// </Summary> private void moveex () {// source file, must exist. String path1 = server. mappath ("goo.txt"); // target file string path2 = server. mappath ("document \ goo.txt"); // moves the specified file to a new location and provides the option to specify a new file name. // if the target file exists, an exception occurs, instead of skipping the move. // The first parameter indicates the path of the source file, and the second parameter indicates the path of the target file. The folder on the path must exist. Otherwise, the IF (file. exists (path1 )&&! File. exists (path2) {file. move (path1, path2) ;}/// <summary> /// rename the file /// </Summary> private void renameex () {// source file, must exist. String path1 = server. mappath (@ "document \ goo.txt"); // target file string path2 = server. mappath (@ "document \ goo1.txt"); // The method for renaming a file is the same as that for moving a file, as long as the target file and source file are in the same folder and the file name is different. If (file. exists (path1 )&&! File. exists (path2) {file. Move (path1, path2 );}}}}