如果不創建目錄,我會檢查目錄是否存在:
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
Animated Gifs 目錄應該放在 D:\Downloaded Images
我可以獲得路徑的姓氏,Radar但我想在沒有子雷達的情況下獲得名稱,或者即使有更多的孩子,D:\Downloaded Images\Radar\1\2\3\4\5我只想獲取D:\Downloaded Images并在下創建一個目錄D:\Downloaded Images
uj5u.com熱心網友回復:
如果要操作檔案夾,可以嘗試使用DirectoryInfo類:
using System.IO;
...
// D:\Downloaded Images\Radar\1\2\3\4\5
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\1\2\3\4\5
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/340337.html
