C#结构如果..如果......如果

时间:2013-10-24 17:27:11

标签: c# io

我使用的是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.");
    }
}  

5 个答案:

答案 0 :(得分: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.");
}

答案 1 :(得分: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.");
}

&安培;&安培;是一个提前退出的运营商。

答案 2 :(得分: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;

您可以通过更改数组长度轻松操纵检查的方式。并且它会准确地打印出路径的哪一部分不正确。

答案 3 :(得分:0)

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

if (Directory.Exists(Path.Combine(sDrive, sFolder, sDivFolder))
    bSanity = true;
else
    Console.WriteLine("FATAL: Div folder doesn't exist.");

答案 4 :(得分: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;
        }
    }
}
相关问题