You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何C# .NET MAUI中File.Exists在Windows和Android平台均失效?

问题描述

我开发了一个C# .NET MAUI应用,在OnAppearing()事件中检查位于Project/Resources/Raw/AboutAssets.txt的源文件是否存在,但File.Exists检测在Windows和Android平台都返回False。

资源文件夹中的AboutAssets.txt

我的代码:

using System.Diagnostics;

namespace SourceFileExists2;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        Debug.WriteLine($"APP DATA DIRECTORY = {FileSystem.Current.AppDataDirectory}");

        string sourceFile = "AboutAssets.txt";
        Debug.WriteLine($"SOURCE FILE = {sourceFile}");
        Debug.WriteLine($"FILE EXISTS 1 = {File.Exists(System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, sourceFile))}");

        string sourcePath = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, sourceFile);
        Debug.WriteLine($"SOURCE PATH = {sourcePath}");
        Debug.WriteLine($"FILE EXISTS 2 = {File.Exists(sourcePath)}");
    }
}

Windows平台输出:

APP DATA DIRECTORY = C:\Users\Jason\AppData\Local\Packages\fd35e565-25c7-4731-bfd1-6267e4eafe80_9zz4h110yvjzm\LocalState
SOURCE FILE = AboutAssets.txt
FILE EXISTS 1 = False
SOURCE PATH = C:\Users\Jason\AppData\Local\Packages\fd35e565-25c7-4731-bfd1-6267e4eafe80_9zz4h110yvjzm\LocalState\AboutAssets.txt
FILE EXISTS 2 = False

Android平台输出:

[0:] APP DATA DIRECTORY = /data/user/0/com.companyname.sourcefileexists2/files
[0:] SOURCE FILE = AboutAssets.txt
[0:] FILE EXISTS 1 = False
[0:] SOURCE PATH = /data/user/0/com.companyname.sourcefileexists2/files/AboutAssets.txt
[0:] FILE EXISTS 2 = False

为什么文件存在性检测在两个平台都无法正常工作?


问题原因

你用FileSystem.Current.AppDataDirectory拼接路径找文件是错误的——Resources/Raw下的文件默认是嵌入到应用程序集里的资源,并不会被复制到AppDataDirectory对应的目录中。File.Exists只能检测文件系统中的物理文件,自然找不到嵌入的资源。

另外要确认文件的生成操作是否设置正确:右键AboutAssets.txt→属性,确保生成操作是MauiAsset(默认配置,但如果被修改过会导致资源无法被正确嵌入)。

正确的检测与处理方法

要检测Resources/Raw下的资源是否存在,需要通过程序集获取嵌入资源的信息;如果需要对文件进行读写操作,可先将资源复制到本地目录再检测。

方式1:直接检测嵌入资源是否存在

protected override void OnAppearing()
{
    base.OnAppearing();

    // 资源名称格式为「项目命名空间.Resources.Raw.文件名」,需和你的项目命名空间匹配
    string resourceName = "SourceFileExists2.Resources.Raw.AboutAssets.txt";
    var assembly = typeof(MainPage).Assembly;
    
    bool exists = assembly.GetManifestResourceInfo(resourceName) != null;
    Debug.WriteLine($"嵌入资源是否存在:{exists}");
}

方式2:将资源复制到本地目录后检测

如果后续需要对文件进行读写操作,可先把嵌入资源复制到AppDataDirectory,再用File.Exists检测:

protected override async void OnAppearing()
{
    base.OnAppearing();

    string sourceFile = "AboutAssets.txt";
    string localPath = Path.Combine(FileSystem.Current.AppDataDirectory, sourceFile);

    // 本地无文件则从嵌入资源复制
    if (!File.Exists(localPath))
    {
        string resourceName = $"SourceFileExists2.Resources.Raw.{sourceFile}";
        var assembly = typeof(MainPage).Assembly;
        
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (FileStream fileStream = File.Create(localPath))
            {
                await stream.CopyToAsync(fileStream);
            }
        }
    }

    // 此时检测本地文件会返回True
    Debug.WriteLine($"本地文件是否存在:{File.Exists(localPath)}");
}

内容的提问来源于stack exchange,提问作者SqueezeOJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:02:51