GetFiles返回空白的.Name属性

时间:2019-06-06 14:39:32

标签: c# asp.net

我正在尝试使用在共享文件夹和所有子文件夹上进行搜索获得的文件名填充ddl。
一切正常,但.Name返回空白。

public JsonResult GetFiles(string MCodeID)
        {
            if (MCodeID == null)
            {
                throw new ArgumentNullException(nameof(MCodeID));
            }

            List<Files> filelist = new List<Files>();
            //Searching Files in //192.168.1.191
            string path = @"\\192.168.1.191\Materials Project\";
            string searchPattern = MCodeID + "*";
            //string[] files = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
            DirectoryInfo fi = new DirectoryInfo(path);
            int FCodeID = 0;
            foreach (var file in fi.GetFiles(searchPattern, SearchOption.AllDirectories))
            {
                filelist.Add(new Files
                {
                    FCodeID = FCodeID,
                    FDescr = fi.Name
                });
                FCodeID += 1;
            }
            filelist.Insert(0, new Files { FCodeID = 0, FDescr = "--Select File--" });
            return Json(new SelectList(filelist, "FCodeID", "FDescr"));
}

文件列表具有2组值,但是两组中的FDescr均为空白。
我将不胜感激,
预先感谢。

1 个答案:

答案 0 :(得分:0)

@Stijn回答

public JsonResult GetFiles(string MCodeID)
        {
            if (MCodeID == null)
            {
                throw new ArgumentNullException(nameof(MCodeID));
            }

            List<Files> filelist = new List<Files>();
            //Searching Files in //192.168.1.191
            string path = @"\\192.168.1.191\Materials Project\";
            string searchPattern = MCodeID + "*";
            //string[] files = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
            DirectoryInfo fi = new DirectoryInfo(path);
            int FCodeID = 0;
            foreach (var file in fi.GetFiles(searchPattern, SearchOption.AllDirectories))
            {
                filelist.Add(new Files
                {
                    FCodeID = FCodeID,
                    FDescr = file.Name
                });
                FCodeID += 1;
            }
            filelist.Insert(0, new Files { FCodeID = 0, FDescr = "--Select File--" });
            return Json(new SelectList(filelist, "FCodeID", "FDescr"));
}