# Visual Basic > Visual Basic .NET > VS 2019 Removing data from an array

## Tesla1886

I am trying to remove several elements from an array. I know that their is no single command  to do that (at least not that I have been able to find. The only way that I have found is to copy the array to another, removing the unwanted data in the process. Is this correct? Is their a better way? If not why am I getting the exception as explained below.

I am using the code below to create or update a text file for recent files opened. However I want to limit that list to a certain number. Up to this point it has been unlimited. The line in red is throwing the following exception. 



```
        Try

            Dim recentStreamWriter As New IO.StreamWriter(RecentFile, True)                'Stream writer to add the most recently opened file to Recent files list (Recent.txt)
            recentStreamWriter.WriteLine(pathToXML)                                        'Add most recent file to Recent.txt
            recentStreamWriter.Close()

            Dim currentFileContents = File.ReadAllLines(RecentFile)                        'Get the list of recently opend files from text file
            Dim FinalFilContents As String()

            For X = currentFileContents.Count To currentFileContents.Count - 10 Step -1
                currentFileContents.CopyTo(FinalFilContents, X)

            Next X

            Dim uniqueratingFiles = FinalFilContents.Distinct()                         'Make sure their are only unigue items, no duplicats

            File.WriteAllLines(RecentFile, uniqueratingFiles)                              'Write the list of recently opened files back to the file Recent.txt

        Catch ex As FileNotFoundException
            If Not File.Exists(RecentFile) Then                                            'If the Recent.txt file does not exist
                File.Create(RecentFile)                                                    'Create it

            End If

        End Try
```



```
Value cannot be null.
```

Thank You

----------


## Shaggy Hiker

Normally, if you are changing the size of an array, then you shouldn't be using an array, but in this case, that's probably not really up to you. ReadAllLines returns an array. You could turn that into a list easily enough, but for removing one item, that really wouldn't gain you much.

However, since you know exactly which item you want to delete, and since it appears to be always the same one, then the loop isn't the most efficient way to do what you want. You already have Array.CopyTo, but take a look at Array.Copy. That will be faster than iterating through an array (not that it really matters, as it will be pretty fast either way). One of the overloads will allow you to copy from one place in one array to one place in a different array. You can copy the first N items in one line, then copy the rest in one other line.

----------


## jmcilhinney

You should explain what you're actually trying to do, rather than how you're trying to do it. It appears that you are trying to maintain a recent files list with a maximum length. I would tend to put the most recent files at the top and the oldest ones would drop off the end. That might look like this:

vb.net Code:
Private Sub UpdateRecentFileList(newFilePath As String, listFilePath As String, maxEntryCount As Integer)
    Dim recentFilePaths As New HashSet(Of String)
     recentFilePaths.Add(newFilePath)
     For Each existingFilePath In File.ReadLines(listFilePath)
        recentFilePaths.Add(existingFilePath)
    Next
     File.WriteAllLines(listFilePath, recentFilePaths.Take(maxEntryCount))
End Sub
The HashSet will ensure that no file path is included twice. We start by adding the new file path to the list, so it appears first. We then add all the existing file paths to the list. If the new file path is already included in the list somewhere, that existing entry will effectively be deleted because it will not be added to the list a second time. We then take the specified number of entries from the top of the list and write them out to the file, overwriting the old list.

----------


## .paul.

> ...We then take the specified number of entries from the top of the list and write them out to the file, overwriting the old list.
> 
> 
> vb.net Code:
> File.WriteAllLines(listFilePath, recentFilePaths.Take(maxEntryCount))


You should note, with a maxEntryCount of 10, and a recentFilePaths.Count of 11, it'll take the first 10 FilePaths.
With a maxEntryCount of 10, and a recentFilePaths.Count of 5, it'll take all of the FilePaths without an error.

----------

