2

Ok, I'm really pulling my hair out on this and have tried a few questions on different things, like on how to debug, but I haven't had much luck so far. Basically, my application comes up, and the user does some things. At some point, the user can save their settings to an output file with the extension of .abc (in raw text format). This extension is registered and becomes editable by the application. So, the user double clicks the file and it brings up the application to the main form view. That all works fine and dandy. If I view the .abc file in notepad, I can see all of my settings that I have saved. My question is, how can you make the application read the several lines of text, and know the program was entered from the file, instead of just selecting to run it from programs? For example, when they save their settings it might look something like this in notepad:

firstname=john
lastname=hancock
address=1234 somewhere road, atlanta, GA  12345

When the user double clicks the file to start the program, I would like the form to recognize there are start up parameters, and fill in the appropriate stuff on the form. So maybe the form will display "Hello john hancock, welcome back" or something of that nature. Is this a possibility? I could make the application force you to select file->open and read the text file in that way, but the preferred method is to do everything in one fail swoop.

Thanks!

user1420914
  • 279
  • 1
  • 4
  • 16
  • 1
    You can check as the file is passed to your program as a command line argument. http://stackoverflow.com/questions/605428/how-can-i-make-an-application-open-when-a-user-double-clicks-on-its-associated-f?rq=1 – Nick Babcock Jul 24 '12 at 19:55

3 Answers3

2

When you start the program up, the filename of the file you used to start it with is in

args[0]

in your main method.

What you can do is you can check to make sure that args.Length > 0 and then load the file with whatever name is in args[0] with whatever file parsing method that you choose.

2

To have your program run when one of its extensions is clicked, use Windows Registry. Registring extensions (their icons and programs) can be done automatically for you if you create a setup project. Take a look here and here.

As in @Sam I am's answer, when the program run receive your arguments specifying what to do with the file.

  • I tried using command line arguments, passing in static void Main(string[] args), but nothing seems to come in the args. Am I missing something? I've already done the part with registering the extension, and just need to get the file path passed into the application so I can do something with it..... – user1420914 Jul 24 '12 at 20:25
  • @user1420914 You might be not passing the argument the right way. `start C:\myapp.exe C:\arg1.txt`. (i.e. the general syntax is `start PROGRAMPATH ARGS`). If you have multiple arguments seperate them by white spaces –  Jul 24 '12 at 20:29
  • In this case, the file name is test.epc (epc is the extension I registered). I just double click that file, and it starts my application up. I'm not sure what happens behind the scenes though. Will double clicking the file pass anything in? – user1420914 Jul 24 '12 at 20:32
  • @user1420914 so is `args.Length` equal to `0` when you double-click the file? – Sam I am says Reinstate Monica Jul 24 '12 at 20:36
  • Yes, I put a message box in there on the main form, that puts the length in there. In the program class, I have it passing args to the main form, and from there it is checking the length..... – user1420914 Jul 24 '12 at 20:41
  • @user1420914 From File Types > Your extension > You will find an item `&Open`. Make sure property `Arguments` for that is "%1" –  Jul 24 '12 at 21:01
  • Well, still got 0 args. Maybe I'll sleep on it. Sometimes that works! :) – user1420914 Jul 24 '12 at 21:06
  • There must some wrong. Please delete all file types you have added. Add a new File type name it `Hello File`. Specify its extension `.epc`. Specify its `Command` to primary output (your app). Make sure there is a menu called `&Open` under `Hello File (.epc)`. From its properties Make sure that `Arguments = "%1"` and `Verb = open`. Now, re-install your app. This should work correctly. –  Jul 24 '12 at 21:11
  • When you say "From its properties make sure that arguments = "%1", where is that from in visual studio express? I am going through project properties->publish tab->Options button->File Associates. In here, I have added .epc extension, progID of epchk, and the icon to use to display it. I couldn't find anything where you can set the arguments to pass, or the verb? Are you talking on an operating system level? – user1420914 Jul 24 '12 at 21:18
  • @user1420914 Then you don't have an action!! Hence it doesn't work. Right click your added extension and click `Add Action`. set `Name` to `&Open My file`, Command to `"%1"`, and Verb to `open`. –  Jul 24 '12 at 21:23
  • I'm sorry, this is my first attempt at any of this, so I'm fairly new at it. The only place I know of to access this, is in the publish options under file associations. However, when I right click a menu doesn't appear. I feel like maybe you are talking about getting to it from a different location. Is that the case? I feel like I'm right on the verge of a great discovery, but can't quite see the light! – user1420914 Jul 24 '12 at 21:27
  • @user1420914 Ok. I am sorry. From visual studio, `File` > `New..` > `Project` > `Setup and deployment` select `Setup project`. We may take so much. let's go chatting –  Jul 24 '12 at 21:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14366/discussion-between-desolator-and-user1420914) –  Jul 24 '12 at 21:30
1

Eureka! I finally found the (a) answer, at least in my environment. I am using visual studio 2010 express edition, and have associated a file with an extension so that when you double click it, it starts the application. In order to get the path to that file, it is stored not as an argument, but in the appdomain property. If you do this command:

String test = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];

The variable test ends up looking like this:

"c:\dir/dir2/dir3/test.abc"

From there, I can read the file and get the contents, or anything else I want to do with it.

I would especially like to thank Desolator. Based on his input, I was better able to search the internet with the right keywords to finally find the answer!

Happy coding!

user1420914
  • 279
  • 1
  • 4
  • 16