首页 > 解决方案 > File.exists 给出一个空引用异常而不是返回 false。C#

问题描述

if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))

此行在找不到文件时始终会导致空引用异常,而不是返回 false。

标签: c#nullreferenceexceptionsystem.io.file

解决方案


File.Exists不是问题。考虑像这样修复您的代码

    if (Application.persistentDataPath != null && Input != null && Input.GetComponent<Text>() != null && Input.GetComponent<Text>().text != null)
    {
        if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))

推荐阅读