English 中文(简体)
3. 在文件夹中搜索特定档案
原标题:Searching particular files in the folder

Hi I can search all the images with .jpg extension and pass it to the fancybox gallery , The issue is i just want the images of particular productid for instance if a product has 5 images , and they are saved as (productid_imagenumber.jpg) , therefore a product with productid 99 will be saved as 99_1.jpg , 99_2.jpg similarly 99_5.jpg, I can pass the productID but i cant find a away just to get the images of that productID , instead of getting all the images which is done by the function below:

Dim directory As DirectoryInfo = New System.IO.DirectoryInfo("C:Images")
        Dim allImages() = directory.GetFiles("*.jpg", SearchOption.AllDirectories)
        Dim strContent As String = ""
        For Each image As FileInfo In allImages

            Dim strTemp As String = (String.Format("<img  src=""{0}"" />", image.Name))
           strContent = "<a class=""fancybox-thumb"" rel=""fancybox-thumb1"" href=""" & image.Name + image.Extension & """ title="""">" & _
            "<img src=""" & image.Name + image.Extension & """ alt="" /></a>"
        Next

        If Not String.IsNullOrEmpty(strContent) Then
            Return String.Format("<div id=""product-lightbox""><p>{0}</p></div>", strContent)
        Else
            Return String.Empty
        End If

    End Function

任何人都可以就如何这样做提出任何建议或协助。

最佳回答

为此:

Dim images = (From img In directory.GetFiles("*.jpg", IO.SearchOption.AllDirectories)
              Where   img.Name.Contains("_") _
              AndAlso img.Name.Split("_"c)(0) = productID).ToList

页: 1 NET 2.0:

Dim allProductIDImages As New List(Of IO.FileInfo)
For Each img As IO.FileInfo In directory.GetFiles("*.jpg", IO.SearchOption.AllDirectories)
    If img.Name.Contains("_") Then
        Dim ID As String = img.Name.Split("_"c)(0)
        If ID.Equals(productID) Then
            allProductIDImages.Add(img)
        End If
    End If
Next

另一种可能更快的做法是让

Dim pattern As String = String.Format("*{0}_*.jpg", productID)
Dim allProductIDImages() As IO.FileInfo = _
       directory.GetFiles(pattern, IO.SearchOption.AllDirectories)
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签