babalae/better-genshin-impact · warning · FileNotFoundException

文件 '{relativePath}' 不存在

Error message

文件 '{relativePath}' 不存在

What it means

Thrown by FileAccessBridge.ReadFile after the path-sandbox check passes but File.Exists(fullPath) is false. The requested file does not exist on disk within the allowed directory.

Source

Thrown at BetterGenshinImpact/Core/Script/WebView/FileAccessBridge.cs:65

            var normalizedPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            return normalizedPath.StartsWith(_normalizedAllowedPath, StringComparison.OrdinalIgnoreCase);
        }
        catch
        {
            return false;
        }
    }

    public string ReadFile(string relativePath)
    {
        try
        {
            var fullPath = Path.Combine(_allowedDirectory, relativePath);
            if (!IsPathAllowed(fullPath))
                throw new UnauthorizedAccessException($"访问路径 '{relativePath}' 被拒绝");

            if (!File.Exists(fullPath))
                throw new FileNotFoundException($"文件 '{relativePath}' 不存在");

            return File.ReadAllText(fullPath, Encoding.UTF8);
        }
        catch (Exception ex)
        {
            throw new Exception($"读取文件失败: {ex.Message}");
        }
    }

    public void WriteFile(string relativePath, string content)
    {
        try
        {
            var fullPath = Path.Combine(_allowedDirectory, relativePath);
            if (!IsPathAllowed(fullPath))
                throw new UnauthorizedAccessException($"访问路径 '{relativePath}' 被拒绝");

            // var directory = Path.GetDirectoryName(fullPath);

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Call FileExists(relativePath) from the webview before ReadFile to handle absence gracefully.
  2. If the file should exist, verify the repo checkout/extraction wrote it; re-import the repo.
  3. On case-sensitive volumes, ensure the requested casing matches the on-disk file name.
  4. Provide a default/empty response to the webview for optional files instead of throwing.

Example fix

// before
if (!File.Exists(fullPath))
    throw new FileNotFoundException($"文件 '{relativePath}' 不存在");

// after (return null/empty for optional reads, or include resolved path)
if (!File.Exists(fullPath))
    throw new FileNotFoundException($"文件 '{relativePath}' 不存在", fullPath);
Defensive patterns

Strategy: validation

Validate before calling

// Probe from the webview before reading
if (!bridge.FileExists(relativePath))
    // handle absence gracefully instead of throwing

Try / catch

catch (FileNotFoundException ex) when (ex.Message.Contains("不存在"))
{
    // return empty/null to webview for optional files
    return null;
}

Prevention

When it happens

Trigger: ReadFile(relativePath): IsPathAllowed returns true, but File.Exists(fullPath) returns false. The path is permitted but the file is absent.

Common situations: A webview script reads a file before it's created; the file was deleted; the path casing on a case-sensitive volume doesn't match; the script expects a file that the repo doesn't ship.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/327b03d126d82898. Report an issue: GitHub.