如何让这个看起来更好看:
lblTotalWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem) * 1024 + " kb"; // Total world size
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem)) // World itself
{
lblWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem) * 1024 + " kb";
}
else
{
lblWorldSize.Text = "Couldn't find world.";
}
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether")) // Nether
{
lblNetherSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether") * 1024 + " kb";
}
else
{
lblWorldSize.Text = "Couldn't find world.";
}
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem + "_the_end")) // The End
{
lblTheEndSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem + "_the_end") * 1024 + " kb";
}
else
{
lblWorldSize.Text = "Couldn't find world.";
}
Run Code Online (Sandbox Code Playgroud)
它真的看起来像一团糟,我似乎无法找到这样的问题.
那么,有几件事可以帮助:
像这样的东西:
// Note that _worldsDirectory must be an absolute path)
string prefix = Path.Combine(_worldsDirectory, selectedItem, selectedItem);
lblWorldSize.Text = GetDirectorySizeOrDefault(prefix, "Couldn't find world");
lblNetherSize.Text = GetDirectorySizeOrDefault(prefix + "_nether",
"Couldn't find _nether");
lblTheEndSize.Text = GetDirectorySizeOrDefault(prefix + "_the_end",
"Couldn't find _the_end");
...
static string GetDirectorySizeOrDefault(string directory, string defaultText)
{
return Directory.Exists(directory)
? GetDirectorySize(directory) * 1024 + " kb"
: defaultText;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我已经更正了您的原始代码始终分配lblWorldSize.Text错误的事实- 我认为这不是故意的.