如何使用没有扩展名的文件夹中的图像名称填充列表框? [英] How do I populate a listbox with the names of images within a folder without the extensions?

查看:85
本文介绍了如何使用没有扩展名的文件夹中的图像名称填充列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以我不确定我需要发布它的格式,但是我想弄清楚的问题是我如何使用文件夹中的图像名称填充列表框在我的Visual Studio项目中。



This my first post so I am not sure about the format that I need to post it, but the problem that am trying to figure out is how I can populate a listbox with the names of the images that I have in a folder within my Visual Studio Project.

protected void Page_Load(object sender, EventArgs e)
       {

           if (!IsPostBack)
           {
               for (int i = 1; i < 8; i++)
               {
                   ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(""));
               }
           }


       }





基本上,我在一个文件夹中有7个图像(在我的实际视觉工作室项目中),我希望for循环用图像的名称填充列表框,但没有扩展名。我似乎无法弄清楚如何使目录工作。如果有人能帮助我,我将非常感激。我遇到了麻烦。



在此之后,我想使用'if'语句根据用户点击的项目设置显示的图像列表框,如果这是有道理的。我不知道如果项目=某个项目的代码,我可以根据他们选择的内容显示图像。



我可能说这真的很糟糕,但是这是我目前遇到的问题,非常感谢任何帮助,谢谢。



Basically, I have 7 images in a folder(within my actual visual studio project) and I want the for loop to populate the listbox with the names of the images, but without the extension. I can't seem to figure out how to get the directory to work. If anyone could help me, I would greatly appreciate it. I am having trouble with this.

After this, I want to use an 'if' statement to set the image displayed based on which item the user clicks on in the listbox, if that makes sense. I do not know the code for if the item = a certain item, I can display an image based on what they select.

I probably worded this really bad, but it's the issue I am currently experiencing, any help would be greatly appreciated, thank you.

推荐答案

删除扩展名使用子字符串和lastindexof





To remove the extension use substring and lastindexof


protected void Page_Load(object sender, EventArgs e)
       {

           if (!IsPostBack)
           {

              String Path = Server.MapPath("/images/");
              String[] FileNames = Directory.GetFiles(Path);


              foreach(string file in FileNames)
              {
                   ListBox1.Items.Add(file.Substring(
                                           0,
                                           System.IO.Path.LastIndexOf(".")
                                           )

                                 );
              }
           }


       }


要获取文件该目录,使用Solution1的修改版本。



创建一个继承自System.IO.FileSystemInfo的类并覆盖所需的方法。 (不幸的是,FileInfo是密封的,所以你不能从该类继承。)

通过重写ToString,你可以在列表框中显示任何值。

To get the files in the directory, use a modified version of Solution1.

Create a class that inherits from System.IO.FileSystemInfo and override the required methods. (Unfortunately FileInfo is sealed so you cannot inherit from that class.)
By overriding ToString, you can show what ever value in the list box.
public class MyFileInfo : System.IO.FileSystemInfo
{
    public MyFileInfo(string fileName)
        : base()
    {
        this.FullPath = fileName;
    }

    public override string ToString()
    {
        return System.IO.Path.GetFileNameWithoutExtension(this.Name);
    }

    public override bool Exists
    {
        get { return System.IO.File.Exists(this.FullName); }
    }

    public override void Delete()
    {
        System.IO.File.Delete(this.FullName);
    }

    public override string Name
    {
        get { return System.IO.Path.GetFileName(this.FullName); }
    }
}







protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string Path = Server.MapPath("/images/");
        foreach (string fileName in Directory.GetFiles(Path))
        {
            // Add a new instance of MyFileInfo for each file.
            ListBox1.Items.Add(new MyFileInfo(fileName));
        }
    }
}



[UPDATE]根据要求提供For-loop变种


[UPDATE] For-loop variant upon request

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string Path = Server.MapPath("/images/");
        string[] fileNames = Directory.GetFiles(Path);
        for (int i=0; i < fileNames.Length; i++)
        {
            // Add a new instance of MyFileInfo for each file.
            ListBox1.Items.Add(new MyFileInfo(fileNames[i]));
        }
    }
}



为了在ListBox中选择项目时显示图像,您可以使用事件


For showing the image when an item is selected in the ListBox, you can use the event

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MyFileInfo mfi = (listBox1.SelectedItem as MyFileInfo);
    if (mfi != null)
    {
        Image img = Image.FromFile(mfi.FullName);
        // Show your image
    }
}





这个解决方案可能有点过分,但它有一个好处,就是跟踪你的文件的完整路径,你可以展示什么是有价值的在列表框行中。



This solution might be a bit of an overkill, but it has the benefit of keeping track of your files full path and you can show what ever value in your list box row.


这篇关于如何使用没有扩展名的文件夹中的图像名称填充列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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