0

So I have developed something in WPF C#. It browses for a file first in order to make the system works. However if the chosen folder is in the wrong file then it throws an error

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

var browse = new System.Windows.Forms.FolderBrowserDialog();
browse.Description = "Select a folder containing *.train files.";
if (browse.ShowDialog() != System.Windows.Forms.DialogResult.OK)
    return;
string[] fileNames = Directory.GetFiles(browse.SelectedPath, "*.train");           
Array.Sort(fileNames);

Thank you in advance!

Zong
  • 6,160
  • 5
  • 32
  • 46
xasmer
  • 1
  • 2
  • What line causes the exception? What does the debugger tell you when you set a breakpoint at that line? – Ken White Apr 17 '17 at 23:54
  • think your array can be an "empty" / 0 sized array, if there are no .train files in the selected directory....but your later code trying to access the bytes of those files, assumes there is always at least 1 – Colin Smith Apr 17 '17 at 23:57
  • The code you've posted here works fine for me (and I have no *.train files). Even setting `browse.SelectedPath = null` before assigning `fileNames` didn't throw any exception. Normally `ArgumentOutOfRange` refers to an index in an array. Are you sure you posted the code that's failing? – Rufus L Apr 18 '17 at 00:05
  • Since code in the post does not actually produce the exception the only suggestion is to look into standard duplicate for this type of exception. To get question re-opened please make sure to provide [MCVE] and provide stack trace along with exception info (plus steps you took to debug the problem based on linked duplicate). – Alexei Levenkov Apr 18 '17 at 00:14

1 Answers1

0
   var browse = new System.Windows.Forms.FolderBrowserDialog();
    browse.Description = "Select a folder containing *.train files.";
    if (browse.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        return;
    if (!string.IsNullOrEmpty(browse.SelectedPath))
    {
        string[] fileNames = Directory.GetFiles(browse.SelectedPath, "*.train"); 
        Array.Sort(fileNames);
    }
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • it still giving me an the same error, but this time its beside the following:
     byte[] filepath= result[result.Count - 1].ImageFile; 
    – xasmer Apr 17 '17 at 23:48
  • show more of your code, as i think the problem is somewhere else – Colin Smith Apr 17 '17 at 23:52
  • change your debugger to "Stop on Exceptions", to isolate/find the exact line - see here: https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx – Colin Smith Apr 17 '17 at 23:54