如何在ASP.NET中创建类似于文件的Web搜索引擎(按名称) [英] How to create a web search engine like for files in ASP.NET (by name)
问题描述
社区你好,
我是开发人员的初学者。
我尝试为文件执行基本的网络搜索引擎在文件服务器上。
1.第一步是按名称搜索文件(如果文件名包含研究字符串,则将其添加到列表框中)。
我的脚本适用于一个文件夹,但它不会转到子文件夹。
如果有人可以帮助我,它将是太棒了
受保护的 Sub Button1_Click(发件人作为 对象,e 正如 EventArgs)句柄 Button1.Click
ListBox1.Items.Clear()
Dim fso,count,src,folder,file
Dim stringtofind
fso = CreateObject ( Scripting.FileSystemObject)
src = D:\DOCUMENTS_MEDECINS \
stringtofind = TextBox1.Text
folder = fso.GetFolder(src)
count = 0
对于 每个文件在 folder.files
如果 InStr(LCase( file.name),LCase(stringtofind))> 0 然后
ListBox1.Items.Add(file.name)
结束 如果
下一步
结束 Sub
2.第二步...在数据网格中显示找到的文件,当你点击文件时它打开它!但目前这只是一个梦想。
感谢您提供的所有帮助。
< blockquote>你正在使用ASP.NET,因此不需要使用古老而过时的Scripting.FileSystemObject。 .NET Framework有许多用于处理文件系统的内置类,主要位于 System.IO
命名空间 [ ^ ]。
例如,要查找给定文件夹的所有子文件夹中的所有文件:
Dim stringToFind As String = TextBox1.Text
Dim sourcePath As 新 DirectoryInfo( D:\ DOCUMENTS_MEDECINS \)
对于 每个文件 As FileInfo 在 sourcePath.EnumerateFiles( *, SearchOption.AllDirectories)
If file.Name.IndexOf(stringToFind,StringComparison.OrdinalIgnoreCase)<> -1 然后
...
结束 如果
下一步
DirectoryInfo [ ^ ] | FileInfo [ ^ ] | EnumerateFiles [ ^ ] | String.IndexOf [ ^ ]
在ASP.NET应用程序中使用硬编码的本地服务器路径不是一个好主意。相反,您应该将该目录作为虚拟目录添加到您的站点,然后使用 Server.MapPath
方法 [ ^ ]转换虚拟路径(例如:〜/ DOCUMENTS_MEDECINS /
)到物理路径。这样,如果您需要将文件夹移动到其他位置,您只需要更新IIS中的映射;你不需要查看代码并更改所有路径。
将文件夹映射为虚拟目录后,链接到文件只是一个将物理路径转换为虚拟路径的情况。没有内置的方法可以做到这一点,但这并不太难:
Dim virtualPath 作为 字符串 = 〜/ DOCUMENTS_MEDECINS /
Dim stringToFind As 字符串 = TextBox1.Text
Dim sourcePath 作为 新 DirectoryInfo(Server.MapPath(virtualPath))
Dim sourceUri 作为 新 Uri(sourcePath.FullName)
对于 每个文件作为 FileInfo 在 sourcePath.EnumerateFiles( *,SearchOption.AllDirectories)
If file.Name.IndexOf(stringToFind,StringComparison.OrdinalIgnoreCase)<> -1 然后
Dim fileUri As 新 Uri(file.FullName)
Dim relativeUri As Uri = sourceUri.MakeRelativeUri(fileUri)
Dim filePath As 字符串 = virtualPath& relativeUri
ListBox1.Items.Add(filePath)
结束 如果
下一步
Server.MapPath [ ^ ] | Uri [ ^ ] | Uri.MakeRelativeUri [ ^ ] < /小>
Hello the community,
I'm a beginner in devellopement.
I try to perform a basic web search engine for files on a files server.
1. The first step is to search file by name (if a file name contain the researched string it add it to the list box).
My script is working for one folder but it doesn't go to the subfolders.
If someone can help me, it will be fantastic
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim fso, count, src, folder, file
Dim stringtofind
fso = CreateObject("Scripting.FileSystemObject")
src = "D:\DOCUMENTS_MEDECINS\"
stringtofind = TextBox1.Text
folder = fso.GetFolder(src)
count = 0
For Each file In folder.files
If InStr(LCase(file.name), LCase(stringtofind)) > 0 Then
ListBox1.Items.Add(file.name)
End If
Next
End Sub
2. The second step ... display the found files in a datagrid and when you click on the file it open it ! But for the moment it's just a dream.
Thanks for all the help you can bring me.
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 theSystem.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 theServer.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[^]
这篇关于如何在ASP.NET中创建类似于文件的Web搜索引擎(按名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!