C # File Association: file transfer with double click on file

I recently made a notepad as a C # program and found a library for using file associations. I am wondering how I will go through the file path by double-clicking in the explorer to the line so that the file can read and β€œopen” a text file (for example, how notepad works). I searched the Internet for a while, and asked several forums and my friends. Any answers or hints in the right direction are appreciated. Thank you.

(note: I already tried reading it with the string[] args parameter in Main() , which was suggested by someone else)

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

+4
source share
3 answers

This works great 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)); } 
+4
source

The sentence was correct, the name of the file that you double-click in Explorer will appear in your application as an args parameter. Then you can do anything with it, for example open a file.

+3
source

I just made the following program

When I open a file using this program, it tells me the path to the file.

 class Program { static void Main(string[] args) { Console.WriteLine(args.Length); foreach (string s in args) { Console.WriteLine(s); } Console.ReadLine(); } } 

the conclusion for me was

 1 C:\Users\MyUserName\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\New Text Document.txt 

you may need to run a similar program using file.openWith to see what happens.

+1
source

All Articles