You're using ASP.NET, so there's no need to use the ancient and obsolete "Scripting.FileSystemObject". The .NET Framework has plenty of built-in classes for working with the file system, mostly located in
the System.IO
namespace[
^].
For example, to find all files in all sub-folders of a given folder:
Dim stringToFind As String = TextBox1.Text
Dim sourcePath As New DirectoryInfo("D:\DOCUMENTS_MEDECINS\")
For Each file As FileInfo In sourcePath.EnumerateFiles("*", SearchOption.AllDirectories)
If file.Name.IndexOf(stringToFind, StringComparison.OrdinalIgnoreCase) <> -1 Then
...
End If
Next
DirectoryInfo[^] | FileInfo[^] | EnumerateFiles[^] | String.IndexOf[^]
Using hard-coded local server paths in an ASP.NET application is not a good idea. Instead, you should add the directory to your site as a "virtual directory", and then use
the Server.MapPath
method[
^] to convert the virtual path (eg:
"~/DOCUMENTS_MEDECINS/"
) to the physical path. That way, if you need to move the folder to a different location, you only need to update the mapping in IIS; you don't need to go through your code and change all the paths.
Once you have the folder mapped as a virtual directory, linking to the file is just a case of converting the physical path to a virtual path. There's no built-in way to do that, but it's not too hard:
Dim virtualPath As String = "~/DOCUMENTS_MEDECINS/"
Dim stringToFind As String = TextBox1.Text
Dim sourcePath As New DirectoryInfo(Server.MapPath(virtualPath))
Dim sourceUri As New Uri(sourcePath.FullName)
For Each file As FileInfo In sourcePath.EnumerateFiles("*", SearchOption.AllDirectories)
If file.Name.IndexOf(stringToFind, StringComparison.OrdinalIgnoreCase) <> -1 Then
Dim fileUri As New Uri(file.FullName)
Dim relativeUri As Uri = sourceUri.MakeRelativeUri(fileUri)
Dim filePath As String = virtualPath & relativeUri
ListBox1.Items.Add(filePath)
End If
Next
Server.MapPath[^] | Uri[^] | Uri.MakeRelativeUri[^]