【问题标题】:to create a gallery in asp.net by iterating through the folders通过遍历文件夹在 asp.net 中创建画廊
【发布时间】:2012-09-24 20:03:45
【问题描述】:

我想在 asp.net 中创建一个画廊,其工作方式如下:

  • 遍历图库文件夹
  • 选择所有文件夹并将它们显示为专辑,封面图片为该文件夹中的 thumbnail.jpg
  • 当点击相册时,它应该会显示文件夹的内容(图片)。

我创建它的方法是遍历文件夹并根据相册创建视图和链接按钮,并使用转发器控件在该视图中将相册的内容显示为图像,但这并没有奏效实施时有很多错误。由于动态视图,我不得不在 on_init() 函数中编写整个内容。我可以实现 html 和 js 部分(如灯箱和其他视觉材料)。 请通过一些代码示例提出一些更好的方法。请使用 c# 。谢谢

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    我不能只给你写所有的代码……而是给你一个启动代码:

    int scanLVL = 4;//or however deep you need to go...
    public  void GetImageFromDir(string sourceDir, int startLVL)
    {
        if (startLVL <= scanLVL)
        {
            // Here you can process files found in the directory.
            string[] fileEntries = Directory.GetFiles(sourceDir);
            Label_showdata.Text +="<br />Dir:" +  Path.GetFileName(sourceDir) ;
            foreach (string fileName in fileEntries)
            {
                // do something with fileName
                String tree = "";
                for (int i = 0; i < startLVL; i++)
                    tree += "&nbsp;&nbsp;&nbsp;";
                Label_showdata.Text += "<br />" + tree + Path.GetFileName(fileName);               
            }
    
            // Going in subdirectories of this directory.
            string[] subdirEntries = Directory.GetDirectories(sourceDir);
            foreach (string subdir in subdirEntries)
    
                if ((File.GetAttributes(subdir) &  FileAttributes.ReparsePoint) !=    FileAttributes.ReparsePoint)
    
                    GetImageFromDir(subdir, startLVL + 1);
        }
    }
    

    这将在您的网页上(实际上在 Label_showdata 中)打印所有文件和它们所在的目录。 基本上从这里开始,您需要将这些数据包装到一个表中,并将其绑定到您觉得舒服的任何控件中......实际上并不难......但是编写所有代码需要更多时间(时间不幸的是我现在没有...)

    【讨论】:

    • 嘿,您的解决方案很好,与我之前所做的相同,但问题在于使用视图和中继器控制来实现它。顺便说一句,我只有 1 级文件夹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多