Search code examples
androidfileunity-game-enginefile-handlingfile-exists

How to check a file exist or not in unity android?


I want to find the file in the path known and if it is pre-existing in android then trigger an event(enable/disable canvas) with it. Here is an example I used -

public void FileChk(){

    string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;

    if (!fileName.Exists)
    {
        //event
    }   
    else
    {       
        //event     
    }
}

what am I doing wrong here and how do I get this event to trigger when the file exists.


Solution

  • You can use the System.IO namespace.

    public void FileChk()
    {
        string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;
    
        if (System.IO.File.Exists(filePath))
        {
            // The file exists -> run event
        }
        else
        {
            // The file does not exist -> run event
        }
    }
    

    The method bool System.IO.File.Exists(string fileName) returns a value indicating if the file exists or not.