Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# File Association: passing double clicked file path to string

Tags:

c#

I recently made a notepad like program for C# and found a library for using file associations. I am wondering how I would pass the path of the file double clicked in explorer, to a string so the file can read and 'open' the text file (like how notepad does). I have googled for a while, and asked around a few forums, and my friends. Any answers or nudges in the right direction are appreciated. Thank You

(note: I've already tried reading it from the string[] args paramater in Main(), which was suggested by someone else)

EDIT: Solved, it was the args[0]. I was really tired when I started on this

like image 674
Lucas Stanesa Avatar asked Feb 20 '23 21:02

Lucas Stanesa


2 Answers

This works fine for me!

public static void Main(string[] args){            
     if (args.Length == 0){
       // Show your application usage or show an error box              
       return;
     }
     string file = args[0];
     Application.Run(new MyProgram(file));           
}
like image 195
GETah Avatar answered Feb 22 '23 11:02

GETah


The suggestion was correct, the filename you double-click on in explorer will be visible in your app as an args parameter. Then you can do what you like with it, such as open a file.

like image 34
Surfbutler Avatar answered Feb 22 '23 11:02

Surfbutler