File.exists提供空引用异常,而不是返回false。 C#

时间:2019-03-29 01:35:13

标签: c# nullreferenceexception system.io.file

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

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

1 个答案:

答案 0 :(得分:0)

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"))
相关问题