File operation methods in C #

Source: Internet
Author: User
Tags file copy

The file class, which is a static class, is mainly used to provide some function libraries. Static utility classes provide a number of static methods that support basic operations on files, including creating, copying, moving, deleting, and opening a file. The parameters of the file class method are often path paths. Some methods of file can return objects of FileStream and StreamWriter. Can be used in support of them. The System.IO.File class and the System.IO.FileInfo class provide a variety of operations about the file, and you need to reference the System.IO namespace when you use it. The main properties and methods are described below through a program instance. (1) File Open method: File.Open () The declaration of this method is as follows: The copy code code is as follows: public static FileStream Open (string Path,filemode mode) The following code opens in the C:\TEMPUPL The OADs directory has a name of newFile.txt file and writes hello to the file. Copy the code code as follows: private void OpenFile () {Filestream.textfile=file.open (@ "C:\tempuploads\newFile.txt", filemode.append); byte [] Info = {(byte) ' H ', (byte) ' E ', (byte) ' L ', (Byte) ' L ', (byte) ' O '}; Textfile.write (info,0,info.length); Textfile.close ();} (2) File creation method: File.create () The declaration of this method is as follows: The copy code code is as follows: public static FileStream Create (string path;) The following code shows how to create a file named NewFile.txt under C:\tempuploads. Because the File.create method grants all users full read/write access to the new file by default, the file is opened with read/write access and must be closed before it can be opened by another application. To do so, you need to use the Close method of the FileStream class to close the file you created. Copy the code as follows: private void MakeFile () {FileStream newtext=file.create (@ "C:\tempuploads\newFile.txt"); Newtext.close ();} (3) File deletion method: File.delete () The method declares the following: the copy code code is as follows: public static void Delete (string path); The following code shows how to delete the NewFile.txt file under the C:\tempuploads directory. Copy the code code as follows: private void DeleteFile () {File.delete (@ "C:\tempuploads\newFile.txt");} (4) File copy method: File.Copy The method is declared as follows: The copy code code is as follows: public static void Copy (string sourcefilename,string destfilename,bool overwrite ); The following code copies c:\tempuploads\newFile.txt to C:\tempuploads\BackUp.txt. Because the overwrite parameter of the Cope method is set to True, if the BackUp.txt file already exists, it will be overwritten by the copy of the previous file. Copy the code as follows: private void CopyFile () {file.copy (@ "C:\tempuploads\newFile.txt", @ "C:\tempuploads\BackUp.txt", True);} (5) file Move method: File.move The method declares the following: the copy code code is as follows: public static void Move (String sourcefilename,string destfilename); The following code can move the BackUp.txt file under C:\tempuploads to the C packing directory. Note: File transfers can only be performed under the same logical disk. An error will occur if you attempt to transfer a file under the C drive to the D drive. Copy the code code as follows: private void MoveFile () {File.move (@ "C:\tempuploads\BackUp.txt", @ "C:\BackUp.txt");} (6) Set file properties method: File.setattributes The method declares the following: the copy code code is as follows: public static void SetAttributes (String path,fileattributes FileAttributes); The following codeYou can set the properties of the file C:\tempuploads\newFile.txt to read-only and hidden. Copy the code code as follows: private void Setfile () {file.setattributes (@ "C:\tempuploads\newFile.txt", fileattributes.readonly| Fileattributes.hidden);} In addition to the commonly used read-only and hidden properties, files include archive (file archive status), System (systems files), temporary (temporary files), and so on. For more information about file attributes, see the description of FileAttributes in MSDN. (7) method to determine whether a file exists: File.exist The method declares the following: the copy code code is as follows: public static bool Exists (string path); The following code determines whether a c:\tempuploads\newFile.txt file exists. If present, copy the file first, then delete it, then move the copied file, if it does not exist, create the file first, then open the file and write, and finally set the file property to read-only and hidden. Copy the code as follows: if (file.exists (@ "C:\tempuploads\newFile.txt"))//Determine if the file exists {CopyFile ();//Copy File DeleteFile ();//delete file MoveFile (); Move file}else{MakeFile ();///Generate file OpenFile ();//Open File Setfile ();//Set file properties} In addition, the file class provides more support for text text. AppendText: Append text to an existing file · CreateText: Create or open a new file for writing text · OpenText: Open an existing text file for reading but the above method mainly operates on the encoded text of UTF-8, thus appearing inflexible. It is recommended that the reader use the following code to operate the TXT file. "read" The TXT file, the sample code is as follows: The copy code code is as follows: StreamReader txtreader = new StreamReader (@ "C:\tempuploads\newFile.txt", System.Text.Encoding.Default); string filecontent; Filecontent = TxtreAder. Readend (); Txtreader.close (); • "Write" The TXT file, the sample code is as follows: The copy code code is as follows: StreamWriter = new Streamwrite (@ "C:\tempuploads\newFile.txt", System.Text.Encoding.Default); string filecontent; Txtwriter.write (filecontent); Txtwriter.close ();

How to file in C # files class

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.