How to open a file with my application?

Well, you know how, in programs like Microsoft Excel or Adobe Acrobat Reader, you can click a file in Explorer and open it using a related program. This is what I want my application to run. Now I know how to set up file associations in Windows so that it knows the default program for each extension. My question is: how can I open the application to open the file by double-clicking on the file.

I searched the Internet using Google, I searched the msdn site, and I searched several forums, including this one, but I did not find anything that explains how to do this. I suppose this has something to do with the parameters of the main method, but this is just an assumption.

If someone can point me in the right direction, I can take it from there. Thanks in advance for your help.

Shane

+4
source share
4 answers

Setting up associations in Windows will send the file name to your application at the command line.

You need to read the event arguments in your main applications in order to read the file path and open it in your application.

See this and this for how to access command line arguments in your main method.

 static void Main(string[] args) { System.Console.WriteLine("Number of command line parameters = {0}", args.Length); foreach (string s in args) { System.Console.WriteLine(s); } } 
+10
source

When you open the file, with associations set as described, your application will be launched with the first argument containing the path to the file.

You can try this in a simple way by printing args from your main method after opening the application by clicking on the appropriate file. The 0th element should be the path to your file.

Now, if you have successfully reached this point, all you need to do now is read the contents of this file. I am sure that you will find more than a lot of resources here on how to do this.

+2
source

I think this is what you are looking for:

 FileInfo fi = new FileInfo(sfd.FileName); //the file you clicked or saved just point //to the right file location to determine //full filename with location info // opening file ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @fi.FullName; startInfo.WindowStyle = ProcessWindowStyle.Normal; Process process = new Process(); process.StartInfo = startInfo; process.Start(); 
+2
source

You will need to create registry keys for your file extension. This page describes well what keys you need to install (see "3. How to create file associations?").

0
source

Source: https://habr.com/ru/post/1338574/


All Articles