列表框中没有扩展名的文件

时间:2019-04-04 22:05:09

标签: c#

1。)如何删除文件扩展名(“ .txt”)? 2.)为什么当我单击列表框窗口的某个位置以便复制所有项目时????

private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DirectoryInfo dinfo = new DirectoryInfo(@"C:\Folder");
            FileInfo[] Files = dinfo.GetFiles("*.txt");
            foreach (FileInfo file in Files)
            {
                listBox1.Items.Add(file.Name);
            }
        }

1 个答案:

答案 0 :(得分:2)

listBox.Items.Clear(); // to clear current listbox items
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
     listBox1.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
}