I making some photo viewer app, so in this class i list all pictures from main app folder at begin program, and from user directories who is opened using openfiledialog, then when user press ok on ofd, i send directory path to properties class PropFields, and set IsFileOpen to true.
Then i check in this class is file open, if is, then i'm trying to filter directory path files, and check is that file picture, i check that using LINQ:
var filter = Directory.GetFiles(path).Where(p => p.ToUpper().EndsWith(ext));
after that i convert var to list, then count list values and send it to PropFields.Max, i use max in main class as limit number of pic's.
then i in string file get value of selected picture, counter has start value as 0, if i press next button it's increment by 1, or if i press previous button it's decremented by 1.
I don't know what is problem here, but i get Exception from third catch block from this code here where is Index out of range?
How is that possible because i convert var to list?
When i start application, i get error and then pic show's normally, then i press next, again error and net pic shows normally, then i go to open some directory where are diff file types, some no pic files which is not in my list, and then i can't list pic's normally, just first pic shows.
Here is code.
private List<string> extensions = new List<string> { ".JPG", ".JPEG", ".PNG", ".BMP", ".ICO", ".GIF", ".TIFF", ".WMF" }; public void GetImage(int counter) { string path = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + @"\images\"; //if openfiledialog returns true if (PropFields.IsFileOpen) { path = Path.GetDirectoryName(PropFields.Path); //new path } try { List<string> lis = new List<string>(); foreach (string ext in extensions) //list extenstions { var filter = Directory.GetFiles(path).Where(p => p.ToUpper().EndsWith(ext)); //filter just pic files lis = filter.ToList(); int max = lis.Count(); //max string file = lis[counter].ToString(); //file is equal selected pic path filtered up PropFields.Max = max; PropFields.Path = file; } } catch (DirectoryNotFoundException ex) { MessageBox.Show(ex.Message); } catch (FileNotFoundException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Sorry for bad English, Thanks.