babalae/better-genshin-impact · warning · FileNotFoundException

文件未找到

Error message

文件未找到

What it means

Thrown by TravelsDiaryDetailManager.IsFileModifiedThisMonth when the given filePath does not exist on disk. The method is called during incremental update logic to check whether a travels diary JSON file was already modified this month, and it expects the file to exist.

Source

Thrown at BetterGenshinImpact/GameTask/LogParse/TravelsDiaryDetailManager.cs:232

        };
        string jsonString = JsonSerializer.Serialize(apiResponse, options);
        string directory = Path.GetDirectoryName(path);

        // 如果目录不存在,则创建它
        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }

        // 将格式化后的 JSON 写入文件
        File.WriteAllText(path, jsonString);
    }

    static bool IsFileModifiedThisMonth(string filePath)
    {
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException("文件未找到", filePath);
        }

        // File.GetLastWriteTime 返回 DateTime 类型为 DateTimeKind.Local
        DateTimeOffset lastModified =
            new DateTimeOffset(File.GetLastWriteTime(filePath)).ToOffset(ServerTimeHelper.GetServerTimeOffset());
        
        // 获取当前月份的开始和结束日期
        DateTimeOffset now = ServerTimeHelper.GetServerTimeNow();
        DateTimeOffset startOfMonth = new DateTimeOffset(now.Year, now.Month, 1, 0, 0, 0, now.Offset);
        DateTimeOffset endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);

        // 判断文件最后修改时间是否在本月
        return lastModified >= startOfMonth && lastModified <= endOfMonth;
    }

    static List<(int year, int month)> GetCurrentAndPreviousTwoMonths()
    {
        List<(int year, int month)> months = new List<(int year, int month)>();

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Return false instead of throwing when the file does not exist — a missing file logically means it was not modified this month.
  2. Ensure the caller always checks File.Exists before calling IsFileModifiedThisMonth, or fold the existence check into the method as a non-throwing guard.
  3. Handle the FileNotFoundException at the call site in UpdateTravelsDiaryDetailManager.

Example fix

// before
static bool IsFileModifiedThisMonth(string filePath)
{
    if (!File.Exists(filePath))
    {
        throw new FileNotFoundException("文件未找到", filePath);
    }
    // ...
}

// after — missing file means not modified this month
static bool IsFileModifiedThisMonth(string filePath)
{
    if (!File.Exists(filePath))
    {
        return false;
    }
    // ...
}
Defensive patterns

Strategy: validation

Validate before calling

// Return false instead of throwing
static bool IsFileModifiedThisMonth(string filePath)
{
    if (!File.Exists(filePath)) return false;
    // ... rest of logic
}

Try / catch

bool modified;
try { modified = IsFileModifiedThisMonth(tddfile); }
catch (FileNotFoundException) { modified = false; }

Prevention

When it happens

Trigger: Calling IsFileModifiedThisMonth(filePath) where filePath was not confirmed to exist beforehand. In the calling code (UpdateTravelsDiaryDetailManager), the file existence is checked before the call only in the i > 0 branch via fileExists, but the method itself redundantly checks and throws if the file vanished between the check and the call (race condition or logic gap).

Common situations: The file was deleted between the File.Exists check and the call (TOCTOU race); the caller passes a path that was never validated; running in an environment where the log directory was cleaned up.

Related errors


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