Reputation: 377
I am following this tutorial on how to allow my program to open files using the "Open With" method found in Windows. However, as soon as the program loads, it crashes with the error "IndexOutOfRangeException".
My code is as follows.
public static void Main(string[] args)
{
if(args[0] != null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Basic_Word_Processor());
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.LoadFile(@args.ToString());
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Basic_Word_Processor());
}
What is causing this exception to occur?
Upvotes: 0
Views: 320
Reputation: 17600
this: args[0]
because when args
are null you are trying to access first element of array that does not exists.
So to fix you program you have to check if args
is not null:
if(args != null && args.Length > 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Basic_Word_Processor());
Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.LoadFile(args[0].ToString());
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Basic_Word_Processor());
}
Upvotes: 5