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

指定路径下授权文件夹检查代码误报未授权文件夹问题排查

代码问题排查与修正

核心问题分析

  1. 路径拼接完全错误
    原代码直接用字符串拼接folderPath + item,生成的路径完全无效。比如item是gameFolder下的某个目录完整路径(比如D:\GTA\anim),加上授权文件夹名anim后会变成animD:\GTA\anim,这种路径不可能存在,必然返回false,触发错误提示。

  2. 循环逻辑混乱

  • 外层遍历授权文件夹列表,内层却遍历gameFolder下的所有目录,每一次循环都弹出提示框,只要有一次不匹配就会触发错误,完全违背“批量检查”的需求。
  • 提示框的标题和图标错误:允许加入的提示用了Error类型的图标和标题,语义完全颠倒。
  1. 需求逻辑误解
    原代码的意图应该是:确保gameFolder下只存在指定的授权文件夹,且所有授权文件夹都存在,但原代码的循环逻辑完全没实现这个目标。

修正后的代码

根据你的需求,这里提供两种场景的修正代码:

场景1:仅检查所有授权文件夹是否都存在(允许存在其他未授权文件夹)

using System.IO;
using System.Windows.Forms;

// 授权文件夹列表
string[] allowedFolders = new[]
{
    "anim",
    "audio",
    "cleo",
    "custom_models",
    "data",
    "libraries",
    "models",
    "modloader",
    "movies",
    "SAMP",
    "text"
};

bool allAllowedFoldersExist = true;

foreach (var folderName in allowedFolders)
{
    // 正确拼接路径:gameFolder + 授权文件夹名
    string fullPath = Path.Combine(gameFolder, folderName);
    if (!Directory.Exists(fullPath))
    {
        allAllowedFoldersExist = false;
        break; // 只要有一个不存在,直接终止检查
    }
}

if (allAllowedFoldersExist)
{
    MessageBox.Show("所有授权文件夹已检测到,你可以加入。", "验证通过", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    MessageBox.Show("缺少授权文件夹,无法加入。", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

场景2:检查所有授权文件夹都存在,且无未授权文件夹(符合你原代码的错误提示逻辑)

using System.IO;
using System.Linq;
using System.Windows.Forms;

string[] allowedFolders = new[]
{
    "anim",
    "audio",
    "cleo",
    "custom_models",
    "data",
    "libraries",
    "models",
    "modloader",
    "movies",
    "SAMP",
    "text"
};

// 获取gameFolder下所有直接子目录的名称(仅取文件夹名,不取完整路径)
var existingFolderNames = Directory.GetDirectories(gameFolder)
    .Select(dir => Path.GetFileName(dir))
    .ToList();

// 检查1:所有授权文件夹是否都存在
bool allAllowedExist = allowedFolders.All(folder => existingFolderNames.Contains(folder));
// 检查2:是否存在未授权的文件夹
bool hasUnauthorized = existingFolderNames.Any(folder => !allowedFolders.Contains(folder));

if (allAllowedExist && !hasUnauthorized)
{
    MessageBox.Show("所有授权文件夹已检测到,无未授权文件夹,你可以加入。", "验证通过", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    string message = "";
    if (!allAllowedExist)
        message += "缺少授权文件夹。\n";
    if (hasUnauthorized)
        message += "检测到未授权文件夹。";
    
    MessageBox.Show(message.Trim(), "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 04:03:04