private void button1_Click(object sender, EventArgs e) { string filePath = textBox1.Text.Trim(); string directoryName = filePath.Substring(0, filePath.LastIndexOf("\"));//根据文件路径获取到该文件夹 string dir = Path.GetDirectoryName(filePath); //获取文件所在的目录 string fileName = Path.GetFileName(filePath); //获取文件名,带后缀 string fileNameNoExt = Path.GetFileNameWithoutExtension(filePath); //获取文件名,不带后缀 string ExtName = Path.GetExtension(filePath); //获取文件的后缀名 string[] Files = Directory.GetFiles(dir, "*.Png"); //获取该目录下所有以后缀名为Png结尾的文件 List<string> files = new List<string>(); foreach (var f in Files) { string nameFull = Path.GetFileNameWithoutExtension(f); string name = nameFull.Substring(0, nameFull.LastIndexOf("_")); if (fileNameNoExt == name) files.Add(f); } string str = string.Join(",", files.ToArray());//以","存在于str中 }