Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net get file names in directory?

Tags:

file

vb.net

I have the following code.

Dim text As String = IO.File.ReadAllText("C:\Example.xtp")

This code is specific to a single file, however I would like to file.readalltext for every file in a particular directory.

How can I achieve this?

like image 456
LabRat Avatar asked May 21 '13 13:05

LabRat


People also ask

How do I get the filename from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.

What is directory GetFiles?

GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

How do I open all files in a folder in Visual Studio?

In Visual Studio, click File > Open > Folder. Navigate to the folder, and click Select Folder. This opens the folder in Solution Explorer and displays its contents, files and any subfolders.

How do I view files in Visual Studio?

Open your project in Visual Studio > click the Show All Files button > expand the bin , Debug > select and right-click the parent folder > choose Include in Project option.


2 Answers

You will need to use the IO.Directory.GetFiles function.

Dim files() As String = IO.Directory.GetFiles("c:\")

For Each file As String In files
  ' Do work, example
  Dim text As String = IO.File.ReadAllText(file)
Next
like image 162
the_lotus Avatar answered Sep 21 '22 08:09

the_lotus


Dim fileEntries As String() = Directory.GetFiles("YourPath", "*.txt")
' Process the list of .txt files found in the directory. '
Dim fileName As String

For Each fileName In fileEntries
    If (System.IO.File.Exists(fileName)) Then
        'Read File and Print Result if its true
        ReadFile(fileName)
    End If
    TransfereFile(fileName, 1)
Next
like image 40
user2933082 Avatar answered Sep 21 '22 08:09

user2933082