绑定Directory.EnumerateFiles到中继器使用泛型列表 [英] Bind Directory.EnumerateFiles to a Repeater using a Generic List

查看:183
本文介绍了绑定Directory.EnumerateFiles到中继器使用泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小的Web应用程序,当我在比赛中键入名称,它将通过一个文件夹中搜索与一大堆的图标,找到一个类似于游戏的类型。

I am trying to create a small web app, that when I type in a game name, it will search through a folder with a whole lot of icons to find one similar to the typed in game.

然后我想显示的图标的名称转发器,以及图标旁边。

I then want to show a repeater with the name of the icon, as well as the icon next to it.

到目前为止,我已经成功地显示的名称与此code:

So far I have successfully displayed the name with this code:

IEnumerable<string> getfiles =
            from f in
                Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories)
            where Path.GetFileName(f).ToLower().Contains(txtGameName.Text.ToLower())
            select Path.GetFileNameWithoutExtension(f);

        List<AllFiles> all = getfiles.Select(file => new AllFiles
            {
                FileName = file, 
                SubdirectoryName = "",
                FileNameWithExtension = ""
            }).ToList();

        rptGameMatches.DataSource = all;
        rptGameMatches.DataBind();

但还没有在显示图标,这将是 ImageLocation 多少运气。

伊夫试图用玩:

foreach (string file in Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories).Count() )
        {
            if(file.)
        }

for (int i = 0; i < Directory.EnumerateFiles(Server.MapPath("~/_resources/game_icon"), "*.*", SearchOption.AllDirectories).Count(); i++)
        {

        }

,但没有成功。

我知道有一个简单的办法做到这一点,但我不能挺似乎得到它的权利。可能有人请告诉我,我要去哪里错了?

I know there is a simplistic way to do this, but I cant quite seem to get it right. Could someone please show me where I'm going wrong?

推荐答案

下面是你可以做一个例子(紧凑性的原因,我已经把后面的code和标记单一的WebForm内,但你当然可以有背后的code在一个单独的文件):

Here's an example of what you could do (for compactness reasons I have placed the code behind and the markup inside a single WebForm but of course you could have the code behind in a separate file):

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<script type="text/c#" runat="server">
    protected void BtnSearchClick(object sender, EventArgs e)
    {
        var images =
            from image in Directory.EnumerateFiles(Server.MapPath(folder), "*.*", SearchOption.AllDirectories)
            let relativeLocation = image.Replace(root, string.Empty).Replace("\\", "/")
            let url = ResolveUrl("~/" + relativeLocation)
            where image.ToLower().Contains(txtGameName.Text.ToLower())
            select new 
            {
                Url = url
            };

        results.DataSource = images;
        results.DataBind();
    }
</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtGameName" runat="server" />
        <asp:LinkButton ID="btnSearch" runat="server" OnClick="BtnSearchClick" Text="Search" />
        <asp:Repeater ID="results" runat="server">
            <ItemTemplate>
                <asp:Image runat="server" ImageUrl='<%# Eval("Url") %>' />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

好吧,所以我们有一个包含一个文本字段,一个按钮和一个中继器的网页。当点击按钮,我们正在寻找那些名称与指定文件夹中的所有图像包含输入到文本字段中的文本:

Alright, so we have a web page containing a text field, a button and a repeater. When the button is clicked we are searching for all images in the specified folder whose name contains the text entered into the text field:

var images =
    from image in Directory.EnumerateFiles(Server.MapPath(folder), "*.*", SearchOption.AllDirectories)
    let relativeLocation = image.Replace(root, string.Empty).Replace("\\", "/")
    let url = ResolveUrl("~/" + relativeLocation)
    where image.ToLower().Contains(txtGameName.Text.ToLower())
    select new 
    {
        Url = url
    };

在这里有几个步骤:

There are a few steps here:

let filename = Path.GetFileName(image)

在此阶段我们得到的物理位置到文件。它看起来是这样的:

at this stage we are getting the physical location to the file. It will look like this:

c:\inetpub\wwwroot\_resources\game_icon\image1.png

然后,我们剥离了应用程序根目录并替换斜杠:

Then we strip the application root and replace the slashes:

let relativeLocation = filename.Replace(root, string.Empty).Replace("\\", "/")

所以 relativeLocation 变成了:

_resources/game_icon/image1.png

和的最后一个步骤是:

let url = ResolveUrl("~/" + relativeLocation)

这将反过来说成:

which will turn that into:

/_resources/game_icon/image1.png

因此​​,在这一天结束的图像采集可能是​​这样的(假设用户搜索图片在文本字段):

[
    { Url: "/_resources/game_icon/image1.png" },
    { Url: "/_resources/game_icon/subfolder1/image2.png" },
    { Url: "/_resources/game_icon/subfolder2/image1.png" },
    ...
]

有此集合被结合到中继器。中继器内部,我们定义了一个图像的的ImageUrl 属性将被绑定到匿名对象的网​​址属性数据,我们构建。

It is this collection that is bound to the repeater. Inside the repeater we have defined an Image whose ImageUrl property will be data bound to the Url property of the anonymous object we constructed.

这篇关于绑定Directory.EnumerateFiles到中继器使用泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆