2

Just like when you make a reference in windows, to open a .txt file, it may open NotePad.exe and or Word.exe, while loading the text from the file into the editor. How can I do this with my Desktop Application. I am having a custom file type for it with extension .mmi. I want it so that when the user double click this file type it will not only open my application, but load the data within it into the appropriate areas of my application. I understand how to set the custom file type for my application settings, but where I am lost is how to get the file info that trigger opening my application so I can get the data from it.

For example. If I open a .html, and choose to use notepad.exe, the html is now loaded in the newly opened text editor.

This is for a From Application, not a console application that starts off with main having args, incase that helps or changes anything.

Example below:

public partial class FormDashboard : Form
{
    public FormDashboard()
    {
        InitializeComponent();
    }

    private void FormDashboard_Load(object sender, EventArgs e)
    {


         //I want to get what file trigger the app to open here, and apply the data accordingly throurght the forms application.

    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Casey ScriptFu Pharr
  • 1,672
  • 1
  • 16
  • 36
  • Why the down vote. I have not seen one answer yet. I understand and do this all the time with console applications, bt how to do it in a Form Application. – Casey ScriptFu Pharr May 19 '14 at 00:11
  • No. I know how to associate a file extension to the app. I need to know how to get wat the file is that was double clicked to open my application, so I can read the info and apply it at start up. I have done this in a console app, but not in a Forms application. – Casey ScriptFu Pharr May 19 '14 at 00:17
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 19 '14 at 00:59
  • Thanks guys, and I apologize if this was a repeat. I kept seeing answers for a console app, which I work with all the time and understood how to do it. I just didn't know how to be able to test this and debug to see what toe Form_Load event held in it. Thanks again everyone. :) – Casey ScriptFu Pharr May 19 '14 at 13:12

3 Answers3

4

The file name will be passed to your application as the first command line parameter. You can get to it using this code:

static void Main(string[] args)
{
    if (args.Length > 0)
    {
        //do stuff with args[0]
    }
}

Or, if you are in WPF, handle the Application.Startup event and get the parameter from e.Args.

vesan
  • 3,289
  • 22
  • 35
4

The answer to your question is no different for a WinForms app and a console app.

The path of the .mmi file that triggered your app will be args[0] in your app's Main method (assuming the signature Main(string[] args)).

So knowing what .mmi file was double-clicked to trigger your app will essentially come for free - after you have told Windows to open .mmi files with your app.

Here is an example - where I just used a text file Test.mmi and a simple console app ConsoleApplication1 for a PoC:

/*
 * Program.cs
 */

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                // Open and display the text in the double-clicked .mmi file.
                Console.WriteLine(File.ReadAllText(args[0]));
            }

            // Pause for 5 seconds to see the double-clicked file's text.
            Thread.Sleep(5000);
        }
    }
}

After telling Windows to open .mmi files with ConsoleApplication1.exe, ConsoleApplication1.exe displays the text in Test.mmi (Whatever....) when I double-click it:

ConsoleApplication1.exe Output

The only thing that will differ from the PoC I have offered is whatever you need do with the file path that comes in as args[0].

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • I unserstand i will be associated by the client user, but i want that data in that file to be et through out the application. For example, if it is a html file with some extra delim fields, I want to load the html into text area, and fill the data from the delim fileds in the file where they are necessary. – Casey ScriptFu Pharr May 19 '14 at 00:10
  • @Casey: Since you asked how to get the file that was double-clicked to trigger your app, that is the focus of the answer. What your app does with the file that was double-clicked is a different matter altogether...that should be addressed distinctly as needed (i.e. with another question if necessary). – J0e3gan May 19 '14 at 00:37
  • For me, I had to set it as "args[1]" instead of "args[0]". Figured this out from docesam's comment in http://stackoverflow.com/a/18448925/3472690 – Kaitlyn Aug 29 '15 at 05:03
1

I think all you need is to read command lines arguments. The file to be opened should be your unique argument.

 class MyClass
    {
    static void Main(string[] args)

    {
         // args[0] = file name to be opened by your application
Fil
  • 1,032
  • 13
  • 29
  • I understand how it would be for a console application using the main(string[] args), but what about in a Forms Application. I can't seem to get the file info from the Form() or Form_Load(Object sender, EventArgs e) methods. – Casey ScriptFu Pharr May 19 '14 at 00:07
  • 3
    If you're in Windows Forms, it's exactly the same. Look into Program.cs, your Main method should be there. – vesan May 19 '14 at 00:10