2

我正在使用 Visual Studio 2013 Express,而且我是 Visual C# 的新手。我确信有更好的方法来做我正在尝试的事情,我将不胜感激任何建议。

我正在尝试编写的代码评估一系列测试并仅在所有测试 = TRUE 时设置一个标志。我目前正在使用六个嵌套的 if 结构来完成这项工作,虽然它有效,但我正在寻找一个更清洁、更专业的解决方案。这是示例(本文缩短为三个级别):

private const string sDrive = "D:\\";
private const string sFolder = "FTP\\";
private const string sDivFolder = "ABC";
private static bool bSanity = false;

private static void SanityCheck()
{
    if (Directory.Exists(sDrive))
    {
        if (Directory.Exists(sDrive + sFolder))
        {
            if (Directory.Exists(sDrive + sFolder + sDivFolder))
            {
                bSanity = true;
            }
            else
            {
                Console.WriteLine("FATAL: Div folder doesn't exist.");
            }
        }
        else
        {
            Console.WriteLine("FATAL: Root folder doesn't exist.");
        }
    }
    else
    {
        Console.WriteLine("FATAL: Disk drive doesn't exist.");
    }
}  
4

5 回答 5

6

主要问题是您是否需要保留与现在相同的错误报告。一般来说,如果需要的话,我认为通过反转案例来处理这更简单。这将允许您使用if/删除嵌套else if

private static void SanityCheck()
{ 
    if (!Directory.Exists(sDrive))
    {
       Console.WriteLine("FATAL: Disk drive doesn't exist.");
    }
    else if (!Directory.Exists(Path.Combine(sDrive, sFolder))
    {
       Console.WriteLine("FATAL: Root folder doesn't exist.");
    }
    else if (!Directory.Exists(Path.Combine(sDrive, sFolder, sDivFolder))
    {
        Console.WriteLine("FATAL: Div folder doesn't exist.");
    }
    else
    {
        bSanity = true;
    }
}  

如果不需要详细的错误报告,您可以直接检查最底层的文件夹:

private static void SanityCheck()
{

    if (Directory.Exists(Path.Combine(sDrive, sFolder, sDivFolder))
         bSanity = true;
    else
        Console.WriteLine("FATAL: Drive or folder doesn't exist.");
}
于 2013-10-24T17:33:26.617 回答
2

使用循环和数组怎么样?

// set path array
var paths = new[] { sDrive, sFolder, sDivFolder };

// use StringBuilder for faster string concatenation
var sb = new StringBuilder();

foreach (var p in paths)
{
    // append next part of the path
    sb.Append(p);

    // check if it exists
    if (!Directory.Exists(sb.ToString()))
    {
        // print info message and return from method, because path is incorrect
        Console.WriteLine("FATAL: \"{0}\" path doesn't exist.", sb.ToString());
        return;
    }
}

// we are here, so the whole path works and we can set bSanity to true
bSanity = true;

您可以通过更改数组长度轻松控制检查的深度。它会准确打印出路径的哪一部分不正确。

于 2013-10-24T17:33:06.007 回答
2
if (Directory.Exists(sDrive) && Directory.Exists(sDrive + sFolder) && Directory.Exists(sDrive + sFolder + sDivFolder))
{
    bSanity = true;
}
else
{
    Console.WriteLine("FATAL: Disk drive doesn't exist.");
}

&& 是一个提前退出操作符。

于 2013-10-24T17:28:27.570 回答
0

因此,一种可能的更清洁的解决方案可能是:

private static List<Tuple<string, string>> _dir = new List<Tuple<string, string>>
{
    Tuple.Create(@"D:\", "FATAL: Disk drive doesn't exist."),
    Tuple.Create("FTP", "FATAL: Root folder doesn't exist."),
    Tuple.Create("ABC", "FATAL: Div folder doesn't exist."),
}

private static void SanityCheck()
{
    var path = string.Empty;
    foreach (var t in _dir)
    {
        path = Path.Combine(path, t.Item1);
        if (!Directory.Exists(path))
        {
            Console.WriteLine(t.Item2);
            break;
        }
    }
}
于 2013-10-24T17:36:49.193 回答
0

这与原始行为并不完全相同,但简单得多。

if (Directory.Exists(Path.Combine(sDrive, sFolder, sDivFolder))
    bSanity = true;
else
    Console.WriteLine("FATAL: Div folder doesn't exist.");
于 2013-10-24T17:29:14.213 回答