Resolved How to open a .txt file from explorer with a C# program?

d.migale9

Member
Joined
Mar 2, 2021
Messages
7
Programming Experience
1-3
Hi everyone, I'm new here and I have a question that I didn't manage to find anywhere.

I'm creating a notepad with C#, and I want to replace it with the stock Windows' notepad. In order to do that, I have to find a way to open .txt files (and others) even if the program is closed: for example, I have a .txt file on the desktop and I want that, when I double click on it, my program runs with that file opened. I hope that my question is clear (I'm not English). The problem is that I have no idea on how to do this. I don't know what can I try, so I don't even have a code to discuss on.

How can I do that? Can you help me? I would really appreciate any kind of help.
 
This is not a C# question. It's a Windows OS question. You just need to update the registry to point to your application instead of the default notepad.exe. Updating the registry is language agnostic. If you don't want to hack the registry, you can use the Windows 10 control panel to setting up the default applications.


 
This is not a C# question. It's a Windows OS question. You just need to update the registry to point to your application instead of the default notepad.exe. Updating the registry is language agnostic. If you don't want to hack the registry, you can use the Windows 10 control panel to setting up the default applications.


I knew it, and I had already done this (I should have specified it in the first message, I'm sorry).

The problem is that, when I double click on the file, it just shows my notepad with no file loaded on it, just like if I opened it through its normal icon. I think there must be some code to implement this functionality, but I don't know it and I hope someone can help me, because my program would be quite useless without this functionality.
 
This is called command line arguments, the first or second argument should be the file path. Depending on which project type this is you can get the arguments from Main method or Environment class. Check if you get argument, if not you have to change something in registry to add that.
 
This is called command line arguments, the first or second argument should be the file path. Depending on which project type this is you can get the arguments from Main method or Environment class. Check if you get argument, if not you have to change something in registry to add that.
I tried this code taken from here, and tried to adapt it to my program:
C#:
 private void Notepad_Load(object sender, EventArgs e)
    {
        string[] LoadedText = Environment.GetCommandLineArgs();
        textBox1.Text = Convert.ToString(LoadedText);
     }

But, unfortunately, nothing happens. Maybe my problem it's not clear, or I'm too unexperienced to understand the articles you sent me and to adapt them to my problem.
 
Show us your registry entries that points to your application. The thing we are particularly interested in is how you setup your "Open" verb.
 
Show us your registry entries that points to your application. The thing we are particularly interested in is how you setup your "Open" verb.
I used the Windows 10 control panel to set up the .txt files to be opened with my application, so I didn't touch the registry.
 
Line 4 will do nothing useful, do like the code example instead.
 
You get console output in Output window in VS (or Immediate window).

Honestly, I have no idea how to use the codes in the link you sent me.
It's a single line of code to output the values, how difficult is that?
 
You get console output in Output window in VS (or Immediate window).
Ok, now I see that: it shows the path of the .exe file of the program. How can I exploit that to sort out my problem?

It's a single line of code to output the values, how difficult is that?
I can't find the link between my problem and that code, that's all.
 
Show use the results. It shouldn't just be the path to your program. It should also include the path to the file that was double clicked.
 
Show use the results. It shouldn't just be the path to your program. It should also include the path to the file that was double clicked.
You're right! I checked better and that's true. It's the second element of the argumens array, so I created a String variable and put the file path in it, then I wrote a simple method which reads the file from that string, and everything works just fine!

Thank you so much and thanks for the patience! :D:D(y)
 
Back
Top Bottom