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

硬盘盘符变更后,程序读取保存路径文件失败的解决方法咨询

解决方案

1. 捕获路径创建异常,避免程序崩溃

当前代码直接调用Directory.CreateDirectory时,若盘符不存在会抛出DirectoryNotFoundException导致程序出错。先给路径创建逻辑加异常捕获,同时提示用户路径无效:

SaveLoadFiles.LoadFile(textBoxRadarPath, "radarpath.txt");
SaveLoadFiles.LoadFile(textBoxSatellitePath, "satellitepath.txt");

bool pathsValid = true;

// 处理雷达路径
if (!string.IsNullOrEmpty(textBoxRadarPath.Text))
{
    try
    {
        if (!Directory.Exists(textBoxRadarPath.Text))
        {
            Directory.CreateDirectory(textBoxRadarPath.Text);
        }
    }
    catch (DirectoryNotFoundException)
    {
        MessageBox.Show($"雷达数据路径 {textBoxRadarPath.Text} 无效,盘符不存在,请重新设置");
        textBoxRadarPath.Clear();
        pathsValid = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show($"创建雷达路径失败:{ex.Message}");
        pathsValid = false;
    }
}

// 处理卫星路径
if (!string.IsNullOrEmpty(textBoxSatellitePath.Text))
{
    try
    {
        if (!Directory.Exists(textBoxSatellitePath.Text))
        {
            Directory.CreateDirectory(textBoxSatellitePath.Text);
        }
    }
    catch (DirectoryNotFoundException)
    {
        MessageBox.Show($"卫星数据路径 {textBoxSatellitePath.Text} 无效,盘符不存在,请重新设置");
        textBoxSatellitePath.Clear();
        pathsValid = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show($"创建卫星路径失败:{ex.Message}");
        pathsValid = false;
    }
}

btnStart.Enabled = pathsValid && (!string.IsNullOrEmpty(textBoxRadarPath.Text) || !string.IsNullOrEmpty(textBoxSatellitePath.Text));

2. 提前验证盘符有效性

在尝试创建文件夹前,先检查路径对应的盘符是否存在,提前规避异常:

先加一个辅助方法:

private bool IsDriveValid(string path)
{
    if (string.IsNullOrEmpty(path)) return false;
    try
    {
        string driveRoot = Path.GetPathRoot(path);
        return DriveInfo.GetDrives().Any(d => d.Name.Equals(driveRoot, StringComparison.OrdinalIgnoreCase));
    }
    catch
    {
        return false;
    }
}

然后修改构造函数逻辑:

SaveLoadFiles.LoadFile(textBoxRadarPath, "radarpath.txt");
SaveLoadFiles.LoadFile(textBoxSatellitePath, "satellitepath.txt");

bool pathsValid = true;

// 验证雷达路径
if (!string.IsNullOrEmpty(textBoxRadarPath.Text))
{
    if (!IsDriveValid(textBoxRadarPath.Text))
    {
        MessageBox.Show($"雷达数据路径 {textBoxRadarPath.Text} 对应的盘符不存在,请重新设置");
        textBoxRadarPath.Clear();
        pathsValid = false;
    }
    else if (!Directory.Exists(textBoxRadarPath.Text))
    {
        Directory.CreateDirectory(textBoxRadarPath.Text);
    }
}

// 验证卫星路径
if (!string.IsNullOrEmpty(textBoxSatellitePath.Text))
{
    if (!IsDriveValid(textBoxSatellitePath.Text))
    {
        MessageBox.Show($"卫星数据路径 {textBoxSatellitePath.Text} 对应的盘符不存在,请重新设置");
        textBoxSatellitePath.Clear();
        pathsValid = false;
    }
    else if (!Directory.Exists(textBoxSatellitePath.Text))
    {
        Directory.CreateDirectory(textBoxSatellitePath.Text);
    }
}

btnStart.Enabled = pathsValid && (!string.IsNullOrEmpty(textBoxRadarPath.Text) || !string.IsNullOrEmpty(textBoxSatellitePath.Text));

3. 可选:自动修复路径

如果用户的文件夹结构在新盘符下存在(比如原D盘内容移到了E盘),可以加自动替换盘符的逻辑:

private string TryAutoFixPath(string oldPath)
{
    if (string.IsNullOrEmpty(oldPath)) return oldPath;
    string oldDrive = Path.GetPathRoot(oldPath);
    // 原盘符存在直接返回
    if (DriveInfo.GetDrives().Any(d => d.Name.Equals(oldDrive, StringComparison.OrdinalIgnoreCase)))
    {
        return oldPath;
    }

    // 获取可用的固定硬盘盘符(排除C盘)
    var availableDrives = DriveInfo.GetDrives()
        .Where(d => d.DriveType == DriveType.Fixed && !d.Name.Equals(@"C:\", StringComparison.OrdinalIgnoreCase))
        .Select(d => d.Name)
        .ToList();

    if (availableDrives.Count == 0) return oldPath;

    // 询问用户是否替换
    var result = MessageBox.Show($"路径 {oldPath} 对应的盘符不存在,是否自动替换为 {availableDrives[0]}?", "路径修复提示", MessageBoxButtons.YesNo);
    if (result == DialogResult.Yes)
    {
        return oldPath.Replace(oldDrive, availableDrives[0], StringComparison.OrdinalIgnoreCase);
    }
    return oldPath;
}

加载路径后调用这个方法:

SaveLoadFiles.LoadFile(textBoxRadarPath, "radarpath.txt");
textBoxRadarPath.Text = TryAutoFixPath(textBoxRadarPath.Text);

SaveLoadFiles.LoadFile(textBoxSatellitePath, "satellitepath.txt");
textBoxSatellitePath.Text = TryAutoFixPath(textBoxSatellitePath.Text);

// 后续验证逻辑同上

4. 持久化修改后的路径

用户修改路径后,要把新路径保存回txt文件,避免下次启动重复出现问题:

给文本框加TextChanged事件处理:

private void textBoxRadarPath_TextChanged(object sender, EventArgs e)
{
    SaveLoadFiles.SaveFile(textBoxRadarPath.Text, "radarpath.txt");
}

private void textBoxSatellitePath_TextChanged(object sender, EventArgs e)
{
    SaveLoadFiles.SaveFile(textBoxSatellitePath.Text, "satellitepath.txt");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 16:55:17