c#如何只显示列表框中类型的certin文件 [英] c# how can you only show certin files of a type in listbox

查看:76
本文介绍了c#如何只显示列表框中类型的certin文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法只显示说.mp3或.zip文件甚至是cdg文件 



我有这个这里的代码 

 public void newlistbox()
{
// geting mods文件夹
checkedListBoxControl1.Items.Clear();
string [] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read(" List_dir"));
string [] dirs = Directory.GetDirectories(kr_base.elfenliedprogsettings.Read(" List_dir"));

foreach(文件中的字符串文件)
{
checkedListBoxControl1.Items.Add(Path.GetFileName((file))); // +" \\" ; + Path.GetFileName((file)));
listboxchoice.Add(file);

}
foreach(dirs中的字符串目录)
{
//waw_mod.Items.Add(Path.GetFileName(dir))b $ b checkedListBoxControl1.Items .Add(Path.GetFileName((dir))); // +" \\" + Path.GetFileName((dir)));
listboxchoice.Add(dir);

}

getname();
}


public void getname()
{
// geting mods文件夹
checkedListBoxControl1.Items.Clear();
string [] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read(" List_dir"));
string [] dirs = Directory.GetDirectories(kr_base.elfenliedprogsettings.Read(" List_dir"));


foreach(文件中的字符串elfy22)
{
checkedListBoxControl1.Items.Add(Path.GetFileName((elfy22)));
}

foreach(dirs中的字符串elfy33)
{
checkedListBoxControl1.Items.Add(Path.GetFileName((elfy33)));
}
}


我只希望它显示eather .zip文件或.cdg文件,因为我正在制作卡拉OK节目,但不知道如何仅仅恭维certin文件显示在列表框中

解决方案


有没有办法只显示说.mp3或.zip文件甚至cdg文件 



我这里有这段代码 

 public void newlistbox()
{
// geting mods文件夹
checkedListBoxControl1.Items.Clear();
string [] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read(" List_dir"));



最简单的方法可能就是使用目录的这种重载。 GetFiles方法:



GetFiles(字符串,字符串)

https://docs.microsoft.com/en-us/dotnet/api/ system.io.directory.getfiles?view = netframework-4.7.2#System_IO_Directory_GetFiles_System_String_System_String_



"返回文件名(包括文件名)路径)匹配指定目录中指定的 

搜索模式。"



请务必阅读"注释",其中说明当您指定正好3个字符的

分机时会发生什么。



如果你需要进一步细化,你总是可以自己解析文件名和$
扩展名。解析一个字符串来查找一个尾随的扩展名并不是很难.b
$
- Wayne


is there a way to display only say .mp3 or .zip files or even cdg files 

i have this code here 

        public void newlistbox()
        {
            // geting mods folder 
            checkedListBoxControl1.Items.Clear();
            string[] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read("List_dir"));
            string[] dirs = Directory.GetDirectories(kr_base.elfenliedprogsettings.Read("List_dir"));

            foreach (string file in files)
            {
                checkedListBoxControl1.Items.Add(Path.GetFileName((file)));// + "\\" + Path.GetFileName((file)));
                listboxchoice.Add(file);

            }
            foreach (string dir in dirs)
            {
                //waw_mod.Items.Add(Path.GetFileName(dir)
                checkedListBoxControl1.Items.Add(Path.GetFileName((dir)));// + "\\" + Path.GetFileName((dir)));
                listboxchoice.Add(dir);

            }

            getname();
        }


        public void getname()
        {
            //geting mods folder 
            checkedListBoxControl1.Items.Clear();
            string[] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read("List_dir"));
            string[] dirs = Directory.GetDirectories(kr_base.elfenliedprogsettings.Read("List_dir"));


            foreach (string elfy22 in files)
            {
                checkedListBoxControl1.Items.Add(Path.GetFileName((elfy22)));
            }

            foreach (string elfy33 in dirs)
            {
                checkedListBoxControl1.Items.Add(Path.GetFileName((elfy33)));
            }
        }

and i only want it to show eather .zip files or .cdg files as i am making a karaoke program but not sure how to impliment only certin files being shown in the listbox

解决方案

is there a way to display only say .mp3 or .zip files or even cdg files 

i have this code here 

        public void newlistbox()
        {
            // geting mods folder 
            checkedListBoxControl1.Items.Clear();
            string[] files = Directory.GetFiles(kr_base.elfenliedprogsettings.Read("List_dir"));

Simplest way is probably to use this overload of the Directory.GetFiles Method:

GetFiles(String, String)
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.7.2#System_IO_Directory_GetFiles_System_String_System_String_

"Returns the names of files (including their paths) that match the specified 
search pattern in the specified directory."

Be sure to read the "Note"s that describe what happens when you specify an
extension of exactly 3 characters.

If you need further refinement you can always parse the filenames and
extensions yourself. Parsing a string to find a trailing extension isn't 
all that difficult.

- Wayne


这篇关于c#如何只显示列表框中类型的certin文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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