0

my application is MVC3 C#; I am populating two dropdownlists using json using the following:

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 });
    }

I am checking if a word exists in a group of files; then display the names of files in one dropdownlist and the lines from each file in the other dropdownlist. It works fine, however it gives me a list of all files becasue I am sending back fnlist. However I am trying to display only the files that contain that word; I could not get the file name from the StreamReader and add it to an array fileList. I would appreciate your suggestions, thanks in advance.

3 Answers 3

1

Already so many lists! Why not another? You already open the file with fnList[i] within the context of the loop, so...

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() 
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Grant, since I am not that experienced; how can I add the arrayList and stateList as per your answer. Thanks
1

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

Later on, we would like to print the name of the file being used by stream reader. eg, if there is an error, I would like a message box that displays "error reading file: 'filename'"

 MessageBox.Show("Error loading " + ((FileStream)file.BaseStream).Name);

Comments

0

Not sure what exactly you are looking for but since you are creating StreamReader from a file name why not have file name in a separate variable and use it later:

var fileName = System.Web.HttpContext.Current.Server.MapPath(
    "/Video/" + fnList[i] + ".srt");
StreamReader file = new StreamReader(fileName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.