从数组中删除项目

Delete Item from array

如何从 目录路径 数组中删除 "FolderName" 项?物理上不是来自路径,仅来自 目录路径 .

string[] directoryPaths = Directory.GetDirectories(@path);

使用列表,而不是数组。

var directoryPaths = Directory.GetDirectories((@path).ToList(); 
directoryPaths.Remove(FolderName);
using System.Linq;

var paths = directoryPaths
  .Where(p => p != folderName)
  .ToArray();

您可以通过找到要删除的元素(例如,在索引 del 处),将所有元素移动 after del 向下移动一个索引,然后通过调用 Array.Resize:

向下调整数组的大小
int del = Array.IndexOf(directoryPaths, @path+"\Desktop Files"); // Pick an index to delete
for (int i = del+1 ; i != directoryPaths.Length ; i++) {
    directoryPaths[i-1] = directoryPaths[i];
}
Array.Resize(directoryPaths.Length-1);