如何在没有 child 的情况下获取部分文件夹路径名?

How can I get part of folder path name without the child?

如果不创建目录,我会检查目录是否存在:

if (textBoxRadarPath.Text != "")
{
    if (!Directory.Exists(textBoxRadarPath.Text))
    {
        Directory.CreateDirectory(textBoxRadarPath.Text);
    
        btnStart.Enabled = true;
    }
}
    
if (textBoxSatellitePath.Text != "")
{
    if (!Directory.Exists(textBoxSatellitePath.Text))
    {
        Directory.CreateDirectory(textBoxSatellitePath.Text);
    
        btnStart.Enabled = true;
    }
}

例如textBoxRadarOath.Text内容是:

 D:\Downloaded Images\Radar

我只想获取部分 D:\Downloaded Images并在此路径中创建一个新名称Animated Gifs

Gif 动画目录应放在 D:\Downloaded Images

我可以获取路径的姓氏 Radar 但我想在没有 child 雷达的情况下获取该名称,或者即使有更多 child 像 [=18] =] 我仍然只想获取 D:\Downloaded Images 并在 D:\Downloaded Images

下创建一个目录

如果你想操作文件夹,你可以尝试使用 DirectoryInfo class:

using System.IO;

...

// D:\Downloaded Images\Radar
DirectoryInfo dir = new DirectoryInfo(textBoxRadarOath.Text);

// Going down up to "D:\Downloaded Images\Radar"
while (!string.Equals(dir.Name, "Radar", StringComparison.OrdinalIgnoreCase))
  dir = dir.Parent;

// Drop "Radar" and Add "Animated Gifs"
dir = new DirectoryInfo(Path.Combine(dir.Parent.FullName, "Animated Gifs"));

如果要检查 Animated Gifs 文件夹是否存在:

// D:\Downloaded Images\Radar
DirectoryInfo dir = new DirectoryInfo(textBoxRadarOath.Text);

while (!string.Equals(dir.Name, "Radar", StringComparison.OrdinalIgnoreCase))
  dir = dir.Parent;

dir = new DirectoryInfo(Path.Combine(dir.Parent.FullName, "Animated Gifs"));

if (!dir.Exists) {
  dir.Create();

  btnStart.Enabled = true;
}