使用 switch 语句创建文件目录?

Use a switch statement to create file directories?

我正在创建一个将安装在许多设备上的应用程序,我想尽可能在​​每台设备的本地磁盘上自动设置文件系统。有没有更简化的方法使用 switch 语句来执行此操作,而不是使用数十个 if 语句?

if (System.IO.Directory.Exists(arctopithecusGalleryPath) == false)
{
    System.IO.Directory.CreateDirectory(arctopithecusGalleryPath);              
}

如何为此创建一个方法:

public void CreateIfNotExists(string path)
{
    if (System.IO.Directory.Exists(path) == false)
    {
        System.IO.Directory.CreateDirectory(path);              
    }
}

然后像这样在您的代码中使用它:

CreateIfNotExists(arctopithecusGalleryPath);

或者如果您有多个目录,您可以将它们添加到列表中并在 foreach 语句中调用此方法:

List<string> folders = new List<string>();
folders.Add("a folder to create");
// add more folders
foreach(var folder in folders)
{
    CreateIfNotExists(folder);        
}

将变量存储在数组中并循环它们以避免多个 IF。

string[] arr1 = new string[] { arctopithecusGalleryPath, arctopithecusGalleryPath1, arctopithecusGalleryPath3 };

for (int i = 0; i < arr1.Length; i++)
{
       if (System.IO.Directory.Exists(array[i]) == false){
       System.IO.Directory.CreateDirectory(array[i]);              
}