0

How can I delete a "FolderName" item from directoryPaths array? Not physically from the path, only from directoryPaths.

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

3 Answers 3

2

You can do this change in place by finding the element that you wish to delete (say, at index del), move all elements after del down by one index, and then resize the array down by calling 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);
Sign up to request clarification or add additional context in comments.

5 Comments

But I don`t know the index of the folder because is not the same every time. I only know the name.
@SebastianCorneliuVîrlan You can use IndexOf method to find the index (see the edit).
int del = Array.IndexOf(directoryPaths, "Desktop Files"); is -1 . So is correct to use Desktop Files or should I provide full path?
@SebastianCorneliuVîrlan Print out the values in a loop, and see which string represents the path that you want deleted. Then supply that path to IndexOf as the second parameter.
int del = Array.IndexOf(directoryPaths, @path+"\\Desktop Files"); please modify the post to make as Answer.
2

Use List, instead of array.

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

Comments

1
using System.Linq;

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

1 Comment

Is correct if: var directoryPaths = directoryPathsx .Where(p => p != @path + "\\Desktop Files") .ToArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.