Determine if the file is empty or exists

Source: Internet
Author: User

  1. /*Introducing the System.IO namespace in the code header*/      //The first of these methods  using(FileStream fs=NewFileStream (@"C:\a.txt", FileMode.Open) {           using(StreamReader sr=NewStreamReader (FS) {if(Sr.) ReadToEnd = =string. Empty) {//is empty             }           }    }        //The second method ofFileInfo fi=NewFileInfo (@"C:\a.txt"); if(FI. length==0){       //is emptyIn contrast, the second method is simpler2This method is suitable for plain text files, such as TXT, ini,html,xml, etc., because they do not include any formatting information.    It is not suitable for files such as Doc,xls,zip, RAR, etc., because even if these files are new empty files, they will include some application information or format information. The best way to differentiate is that you open an application (such as Word, Excel), create a new empty file, and see if its size is0。

  2. if (file.exists (@ "file path")) {//Presence}else{//not present}

Determine if the file is empty or exists

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.