0

我的应用程序是 MVC3 C#;我使用以下内容使用 json 填充两个下拉列表:

public ActionResult CheckWord(string cword)
    {
        try
        {
            List<string[]> arrayList = new List<string[]>();
            List<string[]> stateList = new List<string[]>();
            //
            List<string[]> fileList = new List<string[]>();
            //
            string[] filePaths = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("/Video"), "*.srt");
            string[] fnList = new string[filePaths.Length];
            for (int i = 0; i < fnList.Length; ++i)
            {
                FileInfo fi = new FileInfo(filePaths[i]);
                fnList[i] = fi.Name.Substring(0, fi.Name.LastIndexOf(".srt"));

            }
            int nFiles = filePaths.Length;
            string cacheline = "";
            string line;

            for (int i = 0; i < nFiles; ++i)
            {
                StreamReader file = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("/Video/" + fnList[i] + ".srt"));

                List<string> lines = new List<string>();
                List<string> statments = new List<string>();
                //
                List<string> fnames = new List<string>();
                //
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(cword))
                    {
                        statments.Add(line);
                   //     fnames.Add(file);
                        lines.Add(cacheline);
                    }
                    cacheline = line;
                }
                file.Close();
                var array = lines.ToArray();
                arrayList.Add(array);
                stateList.Add(statments.ToArray());
            }

            return Json(new { success = true, fnList = fnList, arrayList = arrayList.ToArray(), stateList = stateList.ToArray() });
        }
        catch { }
        return Json(new { success = false });
    }

我正在检查一组文件中是否存在一个单词;然后在一个下拉列表中显示文件的名称,并在另一个下拉列表中显示每个文件的行。它工作正常,但是它给了我所有文件的列表,因为我要发回 fnlist。但是我试图只显示包含该单词的文件;我无法从 StreamReader 获取文件名并将其添加到数组 fileList。我会很感激你的建议,在此先感谢。

4

3 回答 3

1

已经有这么多名单了!为什么不是另一个?您已经在循环的上下文中打开了文件fnList[i],所以......

List<string[]> results = new List<string[]>();
....
while ((line = file.ReadLine()) != null) {
  if (line.Contains(cword)) {
    results.Add(fnList[i]);
    break; // optional, if possible, but if you need to continue check for dupes
  }
}
....
return Json(new { 
  success = true, 
  fnList = results.ToArray(),
  arrayList = arrayList.ToArray(), 
  stateList = stateList.ToArray() 
});
于 2013-02-26T17:59:59.560 回答
1

System.IO.StreamReader 文件 = new System.IO.StreamReader("setup.txt");

稍后,我们想打印流阅读器正在使用的文件的名称。例如,如果有错误,我想要一个显示“错误读取文件:'filename'”的消息框

 MessageBox.Show("Error loading " + ((FileStream)file.BaseStream).Name);
于 2014-05-14T23:29:07.990 回答
0

不确定您到底在寻找什么,但是由于您是从文件名创建 StreamReader 为什么不在单独的变量中包含文件名并稍后使用它:

var fileName = System.Web.HttpContext.Current.Server.MapPath(
    "/Video/" + fnList[i] + ".srt");
StreamReader file = new StreamReader(fileName);
于 2013-02-26T18:07:51.173 回答