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);
}
}
答案 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));
}