0

I am getting an error message when taking in command line arguments into an array. The error message states the index was outside the bounds of the array. I've never done this before and found an example online where this works.

This also works with local file paths but not UNC paths. I don't understand why what the string contains would cause an error like this. please help!

private static string[] _allFiles;
private static string _pickup;
private static string _dropOff;
private static int _size;
private static string _sizeInBytes;

static void Main(string[] args)
{
    _pickup = args[0];
    _dropOff = args[1];
    _sizeInBytes = args[2];

    SplitFiles();
}

Error is received at _dropOff = args[1];

System.IndexOutOfRangeException occurred HResult=0x80131508 Message=Index was outside the bounds of the array. Source= StackTrace:

cramopy
  • 3,459
  • 6
  • 28
  • 42
  • 2
    This is because you didn't provide enough arguments to the program on the command line. How are you executing it? – mnistic Oct 16 '17 at 18:14
  • please use a search engine the next time before asking. I knew the duplicate answer just a second after I read your error message... – cramopy Oct 16 '17 at 18:14
  • make sure you give 3 arguments when you call the exe. eg : your_exe.exe pickup_val dropoff_val sizeinbytes – Isham Mohamed Oct 16 '17 at 18:14
  • @mnistic I am providing it three arguments with spaces in between each one. – funktail1989 Oct 16 '17 at 18:17
  • from what @cramopy posted above it makes it look like i didn't initialize my array but i didn't think that was something i needed to do when it is only being used inside of the method and then assigning to class variables. – funktail1989 Oct 16 '17 at 18:17
  • @funktail1989 no, you don't *call* your function with the appropriate amount of arguments: you are calling it with to less (must be lower then 3) unless there wouldn't be the error message... A sample call must look like this: `yourProgramName.exe "bli" "bla" "blu"` (you can omit the quotes, if you don't have any whitespace within the parameter values) – cramopy Oct 16 '17 at 18:20
  • @cramopy FileSlicer.exe "\\domain\path\shared\folder1\folder2\folder3\" "\\domain\path\shared\folder1\folder2\folder3\NEW" 750000 This contains all three parameters that i'm passing it. 2 UNC file paths, where it is picking up and dropping off and the size of the file in bytes. – funktail1989 Oct 16 '17 at 18:26
  • Which line throws an error? Can you provide [minimal, complete verifiable example](https://stackoverflow.com/help/mcve)? – Vikhram Oct 16 '17 at 18:32
  • the error is thrown on _dropoff line – funktail1989 Oct 16 '17 at 18:34
  • @funktail1989 what environment are you running on? Linux or Windows? On Windows a \ (backslash) always has to be escaped (e.g. doubled) otherwise `\"` could be interpreted as a escaped quote (e.g. a quote-char in a quoted string). Please debug into your code and see what values are inside the `args` array. – cramopy Oct 16 '17 at 18:34
  • when debugging the code _dropOff is still null because it is stopping there but _pickup is \\\\domain\\path\\shared\\folder1\\folder2\\folder3\\ – funktail1989 Oct 16 '17 at 18:38
  • so it looks like args[0] actually contains the pickup, dropoff and file size arguments i'm passing to it. I'm confused. apparently i don't know how to separate each index in the array to their values – funktail1989 Oct 16 '17 at 18:42
  • Evidence that a trailing slash in the first argument can cause problems interpreting arguments: https://stackoverflow.com/questions/653563/passing-command-line-arguments-in-c-sharp – Trioj Oct 16 '17 at 18:53
  • so adding an extra \ at the end does successfully seperate the indexes and resolve that issue. now other to other issues. Thank you all – funktail1989 Oct 16 '17 at 19:16

0 Answers0