Blog

Double Click on Your File(extension) & open it with your EXE(application)

By Tanmay Sarkar Jul 22 2010
When we double click on that file(example.ourextesion) & browse to open with our *.exe a problem will occured! That is our exe will open but contain are not shown. So, what's the problem??
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 14k
  • 0
We can build our simple texteditor with the help of C#, we also add option for save file open file(with our own extension).
But when we double click on that file(example.ourextesion) & browse to open with our *.exe a problem will occured!
That is our exe will open but contain are not shown. So, what's the problem??

The problem is when we double click to open our *.exe,
but no one triger to our *.exe to open our file(example.ourextesion) ! - that's the problem!
So what will we do??

I t's very eassy to solve this problem, I upload a sample project for your understanding.
The main thing which we forget I writedown here.
We should look 2 things.
1.check the string args[] value in Program .cs &
2.write a consturcter to load the file in Form1.cs

In Program.CS file, the default code is:

 static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

It will modified to:

 static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
Application.Run(new Form1(args[0]));
else
Application.Run(new Form1());
}

Now in your Main Form here is Form1, a constructer will need to open a file for double click.
The code is as folllows to open a file in RichtextBox,

 public Form1(string filename)
{
InitializeComponent();
if (filename != null)
{
try
{
richTextBox1.LoadFile(filename);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

Now you can open your extension (like *.tanmay) on double click your file in any location of your computer

Note: For first time browse your *.exe to open your file which have your own extension.
If you have registry idea then you do it in registry value.

Hope it will help you,
Thank You!