I am working on migration of legacy Windown form app wrriten in vb.net to .net core api (C#).
There is as an code statement written in VB.net to search the file in directory
Function HasDoc(ByVal branch As String, ByVal sender As String, ByVal appName As String) As Boolean
Dim strPath = "T:\" + appName + branch + "\" + sender + "\_" + branch + "_*.*"
dim ls_files = Dir(strPath, FileAttribute.Archive)
dim bolExist as Boolean
If ls_files = Nothing Then
bolExist = False
Else
bolExist = True
End If
return bolExist
End Function
I am using c# statement to replicate below line of code
dim ls_files = Dir(strPath, FileAttribute.Archive)
C# Code
public bool HasDoc(string appName, string branch, string sender)
{
var filesPath = "T:\\" + appName + branch + @"\" + sender;
var strPatt = "_" + branch + "_*.*";
if (System.IO.Directory.Exists(filesPath))
return Directory.EnumerateFiles(filesPath, strPatt).Any();
return false;
}
Code I written in C#, performance is very slow and also I am not sure how to use FileAttribute.Archive? where legacy code performance comparatively faster than mine, the file location where above code searching the files contain millions of documents.
Please suggest what I can use to replicate the same Vb.net code behaviour
The call
Dir(strPath, FileAttribute.Archive)searches for a file that matches the pattern and has theArchiveattribute set.Diris actually a VB6 function that remained in VB.NET only to ease migration to the new environment. Underneath it uses the same classes that C# does, eg DirectoryInfo.GetFileSystemInfos. It's not possible to filter by attribute, so the implementation loops over all the files and checks their attributes.The
Microsoft.VisualBasic.*convenience libraries allow you to use the same functions as VB.NET, eg Dir but these are rarely used nowadays.You can use the
DirectoryInfoclass in C# directly.GetFileSystemInfosreturns an array with objects representing all contents of a folder, both directories and files. You can useEnumerateFileSystemInfosinstead to return anIEnumerable<>that only returns items as you read them. Since you only want files, you can useEnumerateFilesinstead ofEnumerateFileSystemInfos. This returns anIEnumerable<FileInfo>instead of astring[], which allows you to check a file's attributes.For example:
You can swap
EnumerateFileswithGetFilesor evenGetFileSystemInfosto see if this affects performance.All these functions accept an EnumerationOptions parameter that can be used to specify recursion depth, buffer sizes etc.