Remove all files in the file directory and view the contents of the file in unity

Source: Internet
Author: User

This script stores the following:

How to delete all the files in a folder,

Ways to delete individual files

Ways to get text content

Ways to get other types of text content

Ways to write to a text file

voidStart () {stringFilePath = Application.streamingassetspath +"/"+"Abb.txt"; #regionTo delete a file//string fullPath = Application.streamingassetspath; //bool isTrue = Deleteallfile (FullPath); //if (isTrue)//{    //Debug.Log ("Delete successful!!!!!!"); //}    //Else//{    //Debug.Log ("Delete failed!!!!!!"); //}    #endregion     #regionThe first way to get the contents of a file//string m_str = Gainfilecontent_1 (FilePath); //Debug.Log (m_str);    #endregion     #regionThe second way to get the contents of a file//string m_str = Gainfilecontent_2 (FilePath); //Debug.Log (m_str);    #endregion     #regionThird way to get the contents of a filestringM_STR =gainfilecontent_3 (FilePath);    Debug.Log (M_STR); #endregion     #regionTo write the contents of a fileBOOLIsTrue =writefilecontent_3 (FilePath); if(isTrue) {Debug.Log ("Successful WRITE!!!!!!"); }    Else{Debug.Log ("Write failed!!!!!!"); }    #endregion}/// <summary>///Delete all files under the specified file directory/// </summary>/// <param name= "FullPath" >file path</param> Public BOOLDeleteallfile (stringFullPath) {    //gets all resource files below the specified path and then deletes them    if(Directory.Exists (FullPath)) {DirectoryInfo direction=NewDirectoryInfo (FullPath); fileinfo[] Files= direction. GetFiles ("*", searchoption.alldirectories); Debug.Log (Files.         Length);  for(inti =0; I < files. Length; i++)        {            if(Files[i]. Name.endswith (". Meta"))            {                Continue; }            stringFilePath = FullPath +"/"+Files[i].            Name;            Print (FilePath);        File.delete (FilePath); }        return true; }    return false;} /// <summary>///First: Gets the contents of the specified file/// </summary> Public stringGainfilecontent_1 (stringFilePath) {    stringM_STR =NULL; Try    {        //reads all the rows of the file and reads the data into the defined character array strs, one cell in a row        string[] STRs =File.ReadAllLines (FilePath);  for(inti =0; I < STRs. Length; i++) {m_str+ = Strs[i];//read each line and connect it togetherM_str + ="\ n";//line breaks at the end of each line        }        returnm_str; }    Catch(System.Exception e) {Debug.Log (e.message); returnm_str; }     } /// <summary>///get content from a text document or a non-TXT text document/// </summary>/// <param name= "M_filename" >the path and name of the file</param> Public stringGainfilecontent_2 (stringm_filename) {    Try    {        stringPathsource =M_filename; using(FileStream Fssource =NewFileStream (Pathsource, FileMode.Open, FileAccess.Read)) {            byte[] bytes =New byte[Fssource.length]; intNumbytestoread = (int) Fssource.length; intNumbytesread =0;  while(Numbytestoread >0)            {                intn =fssource.read (Bytes, numbytesread, numbytestoread); if(n = =0)                     Break; Numbytesread+=N; Numbytestoread-=N; } numbytestoread=bytes.            Length; stringM_STR =UTF8Encoding.UTF8.GetString (bytes); returnm_str; }    }    Catch(System.Exception e) {Debug.Log (e.message); return NULL; }} /// <summary>///to read the contents of a text/// </summary>/// <param name= "FilePath" ></param>/// <returns></returns> Public stringGainfilecontent_3 (stringFilePath) {    //ReadAllText method The first parameter is the path to read the TXT file, and the second parameter is the encoding method, which uses the default    stringM_STR =File.readalltext (FilePath, Encoding.default); returnm_str;} /// <summary>///write a file to the text/// </summary>/// <param name= "FilePath" ></param>/// <returns></returns> Public BOOLWritefilecontent_3 (stringFilePath) {    Try{file.appendalltext (FilePath,"I was written in.", Encoding.default); return true; }    Catch(System.Exception e) {Debug.Log (e.message); return false; }    }

The above realized for the text file increase, delete, check, hope to help everyone!!!!!!

Remove all files in the file directory and view the contents of the file in unity

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.