-1

I have .xml file to store my data from my application.

Now I would like to click on the .xml file -> start my application with data loaded from the .xml file.

What makes application to run the xml parsing metods ?

I have no idea how to do this, what exactly happens when I open .xml file with my application ? Does Windows fetch the application some kind of parameter with .xml file path or something ? Also WPF does not seem to have the Main() method which usually handles these parameters.

Any idea how to do it ?

Safiron
  • 883
  • 4
  • 11
  • 30
  • It's know as a [file association](http://en.wikipedia.org/wiki/File_association). – 500 - Internal Server Error Oct 07 '14 at 22:39
  • 1
    Use `Environment.GetCommandLineArgs()`, which will return a `string[]`. Index 0 will be the path of your app, and index >=1 will have arguments. Thus, if you open a file associated with your app, or if you click -> drag a file onto your executable, the application will run with 2 parameters (the file path of the app in [0] and the file path of the file in [1]). You can use this file path at index [1] to open your xml file when your application starts. Hope this makes sense. – learningcs Oct 08 '14 at 05:03
  • rshepp - work perfectly thanks. Write your answer so I can give you points – Safiron Oct 11 '14 at 10:45

3 Answers3

0

First of all you can't run an application by clicking xml file. What you would do would be to have some method invoked via button click or any event which reads in xml file through file reader or stream reader and take those contents and have xmldocument doc = new xmldocument(). Then doc.LoadXml(filecontents). Then you can parse through document using built in C#.NET methods such as doc.SelectSingleNode(node name).

Maxqueue
  • 2,194
  • 2
  • 23
  • 55
0

If you are trying to find out the program associated with a particular file extension, then there are 2 ways to do this programmically. The first way is through executing a Win32 method called FindExecutable. The DllImport looks like this:

[DllImport("shell32.dll")]
static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult);

The second way is to look in the registry. To find out which application double-clicking on an XML file would open, you'd look in HKEY_CLASSES_ROOT\.xml. Then you follow that entry to the shell/open/command. That will give you the application name and path that opens the file.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
0

So here is what I did:

I made my own file extension .xxx (inside there is just plain xml) when I open it with my application I check in constructor of startup window for the parameters count using the

Environment.GetCommandLineArgs()

If count > 1 I get the second parameter which is string with absolute path to the file. Then I fetch the string to the method handling the opening of the file.

Safiron
  • 883
  • 4
  • 11
  • 30