无法从'System.Collections.Generic.List <string>'转换为'string'

时间:2015-12-12 01:33:57

标签: c#

问题多次回答,但我不知道如何解决这个问题!

    string path = comboBox1.GetItemText(comboBox1.SelectedItem).Substring(0, 1) + (":\\");
    var ext = new List<string> { ".tmp", ".vbs", ".lnk" }
    foreach (string file in Directory.GetFiles(path, "*" + ext, SearchOption.AllDirectories))
        if (file.EndsWith(ext,  StringComparison.OrdinalIgnoreCase))
        {
            MessageBox.Show(ext);
        }

3 个答案:

答案 0 :(得分:0)

尝试使用:

if (ext.Any(x => file.EndsWith(x))
    MessageBox.Show(ext);

答案 1 :(得分:0)

你应该迭代扩展:

    var extensions = new List<string> { ".tmp", ".vbs", ".lnk" };

    foreach (var ext in extensions) {

        var files = Directory.GetFiles(path, "*" + ext, SearchOption.AllDirectories);

        foreach (string file in Directory.GetFiles(path, "*" + ext, SearchOption.AllDirectories)) 
        {
            if (file.EndsWith(ext, StringComparison.OrdinalIgnoreCase))
            {
                 MessageBox.Show(ext);
            }
       }
   }

答案 2 :(得分:0)

这样的事情应该这样做:

var extensions = new List<string> { ".tmp", ".vbs", ".lnk" }
foreach (string ext in extensions) {
  foreach (string file in Directory.GetFiles(path, "*" + ext, SearchOption.AllDirectories)) {
    if (file.EndsWith(ext, StringComparison.OrdinalIgnoreCase))
    {
      MessageBox.Show(ext);
    }
  }
}