0

我使用此代码打开文件夹并从中浏览文件,但我需要在没有打开文件夹的情况下获取这些文件,当我运行我的程序时会自动加载这些文件,我尝试使用GetFiles()但在应用过滤器时没有与我一起使用对于我要选择的文件,
这是我的代码

OpenFileDialog dlg = new OpenFileDialog();             
dlg.InitialDirectory = @"C:\Users\ahmed\Desktop\samples";
dlg.Filter = "Gestures (*.xml)|*.xml";
dlg.Title = "Load Gestures";
dlg.RestoreDirectory = false;
dlg.Multiselect = true;

if (dlg.ShowDialog(this) == DialogResult.OK)
{
    for (int i = 0; i < dlg.FileNames.Length; i++)
    {
        string name = dlg.FileNames[i];
        _rec.LoadGesture(name);
    }
    ReloadViewForm();
}
4

1 回答 1

2

尝试使用Directory.GetFiles。它返回指定目录中的文件名(包括它们的路径)。

var files = Directory.GetFiles("C:\\");

foreach (var file in files)
{
    var fileInfo = new FileInfo(file);
    Console.WriteLine(fileInfo.Name);
}
于 2015-05-24T11:43:11.780 回答