Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am an artist who likes to code. I code in vs2010 and with winapp.
I created a (text) file "myfile.myapp"
Now, I made this file to open with my application.
What is did so far: Right click on my file - Open with - Choose default Program - ('Open With' window appears) - Browse - (find my winapp.exe) and click on it.
Now my file is linked to open only with this application.
But the lough is when my application starts. It doesnt know how to receive the file i just put to open with it.
I have no idea how to make the code for it, and where to put this code.
My impression is to put the code in the
public Form1()
       {
           InitializeComponent();
       }

But what kind of code? I am thinking to read from a stream. But again, how?
Thank you so much for your help and happy new year 2020.

What I have tried:

----------------------------------------------------
Posted
Updated 23-Dec-19 3:49am
v2

1 solution

When you open your app through explorer, it is passed to it via the command line arguments. So try this in your form code:
C#
string[] args = Environment.GetCommandLineArgs();
string filename = "";
if (args.Length > 1)
    {
    filename = args[1];
    ...
    }
args[0] will always be the executable that is running.
 
Share this answer
 
Comments
_Q12_ 23-Dec-19 10:02am    
Thank you VERY much mister originalGriff; Always a pleasure getting help from you.
The code worked like a charm.

And here is how i implement it for future coders:
public Form1()
{
InitializeComponent();

string filename = "";
if (args.Length > 1)
{
filename = args[1];
text = File.ReadAllText(filename);
}

richTextBox1.Text = text;
}

string text = "";
string[] args = Environment.GetCommandLineArgs();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900