Paul T.
Paul T.

Reputation: 35

How can I open a text file with my executable?

I want to right-click on a text file and "Open With..." it with my own program, but I can't find any information on how to do that. I want to make my program in C++ or with WinForms (C#). I want to open that file and to use my program as an interpreter on a small "homemade programming language", so I want to pass the data from the file directly to my program. Can anyone help me? *hope I'm clear enough on what I'm trying to do.

Upvotes: 1

Views: 870

Answers (1)

Bennet
Bennet

Reputation: 56

I'm just gonna to answer your Question for C#. If you still need C++ support you can tell me.

Option 1 - Drop down:

So if you for example create a Console-Application in C# (Visual Studio), it will look like this:

enter image description here

As you can see in the Picture: the Program accepts Arguments (args String Array) If you drag & drop your file on your .exe, the filepath of the file you dropped will be saved in the args String Array. Now you can read the file (for example with the File-Class).

Option 2 - Right Click -> Open with my Program:

For that, you can simply add a new entry in HKEY_CLASSES_ROOT\Directory\Background\shell (Windows Registry) to register you Program as a "Right Click Menu Program". Here is a detailed How-To:

https://www.howtogeek.com/howto/windows-vista/add-any-application-to-the-desktop-right-click-menu-in-vista/

After you added your Program to the Windows Registry you can proceed as shown in Option 1 (args).

Any more questions? Let me know.

Greets Bennet

EDIT: Sorry, didnt really read the comments :D but i guess your Question is answered. I will let this stay here for future readers which dont read the comments either ;)

Upvotes: 3

Related Questions