从数组中删除项目

时间:2015-01-12 00:10:34

标签: c# winforms visual-studio-2010

如何删除" FolderName"来自 directoryPaths 数组的项目?不是来自路径,而是来自 directoryPaths

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

3 个答案:

答案 0 :(得分:2)

使用List,而不是数组。

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

答案 1 :(得分:2)

您可以通过查找要删除的元素(例如,在索引del处)进行此更改,将 del之后的所有元素向下移动一个index,然后通过调用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);

答案 2 :(得分:1)

using System.Linq;

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