How to convet the c# to VB.NET

Steven Young 261 Reputation points
2021-01-28T08:44:42.987+00:00

var files = Directory.GetFiles("C:\path", ".", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));

How to convert to VB.NET?

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-01-28T09:02:50.163+00:00

    For example =>

    Dim files = Directory.GetFiles("C:\path", ".", SearchOption.AllDirectories).Where(Function(f) f.EndsWith(".mp3") OrElse f.EndsWith(".jpg")).ToArray
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-28T15:21:03.353+00:00

    The following may not be for you as it's really for large folder structures.

    If by chance you have a large folder structure you may consider a different approach that keep the UI responsive. In the screenshot below the code in this case uses recursion to find *.png, *.exe (I used different extensions from you simply because I don't have any mp3 or jpg files). The code presented has plenty of room for customization.

    Coded in VS2019

    Front-end project, back-end project.

    Shows what was searched folder-wise

    61494-11111111111.png

    Shows results (you could add the path on, in the code the path is available)

    61479-11111111111-1.png

    0 comments No comments

  2. Sam of Simple Samples 5,546 Reputation points
    2021-01-28T18:50:38.003+00:00

    You can use C# to VB.NET Converter for free for output up to 100 lines per file. There is a snippet option that can convert snippets such as you are asking about.

    0 comments No comments

  3. Steven Young 261 Reputation points
    2021-01-29T01:37:53.817+00:00

    There's the function below

    1.    Dim wildcards As String() = {"*.mp3", "*.jpg"}
        Dim ListFiles As List(Of String) = FileIO.FileSystem.GetFiles("G:\Music", FileIO.SearchOption.SearchAllSubDirectories, wildcards).ToList
    

    But the result is different with

    1. Dim files = Directory.GetFiles("G:\Music", ".", SearchOption.AllDirectories).Where(Function(f) f.EndsWith(".mp3") OrElse f.EndsWith(".jpg")).ToArray

    I used two functions to get my music folder, NO1 return 617, NO2 return 593, can you test on your side? thank you.

    0 comments No comments

  4. Steven Young 261 Reputation points
    2021-01-29T02:30:56.21+00:00

    The C# code below is easy for more file types.

    var allowedExtensions = new [] {".doc", ".docx", ".pdf", ".ppt", ".pptx", ".xls", ".xslx"};
    var files = Directory
    .GetFiles(folder)
    .Where(file => allowedExtensions.Any(file.ToLower().EndsWith))
    .ToList();

    How to convert to VB.NET?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.