C#: How do you handle/parse messages for your applications like drag and dropping an associated file type?
Let’s say I have a text document application and I want it to execute and open a file if,
1.) a .txt document is dragged on top of the exe.
How could I make that possible, to execute the text software, and two, finally open and display the text document in the text document software?
You have to find where your Main method is declared to be sure its signature includes the args parameter, then you can check upon args array and you will find the complete pathname of the file dragged on your application’s exe. Now you can then work with it accordingly to your needs.
Example:
You can also drag more than a file and find their names inside the same array. Remember that in my example i don’t check if there are actually some elements in args array and thus i can get an IndexOutOfBoundException if nothing is dragged (or passed as argument) when launching the application and finally that using Path.GetExtension method doesn’t assure you the file is what you think, but just it has that extension.