views:

164

answers:

4

hello everybody, first: i'm dutch so sorry if my english is not so good.

I have made my own file type (.ddd) and I made a simple program to open this file type, but wenn i click on a .ddd file (on my desktop) my program opens only the file is not automaticly opend inside my program.

how do I directly open the file in my program when it opens?

+1  A: 

The windows shell passes the filename to your program as a command-line argument. Your program needs to read its command-line arguments and open the file specified there.

Conrad Albrecht
Only if he used drag and drop or if his program is registered to handle that file type. I think he wants to know how to do the latter at install time.
Joel Coehoorn
but the question says that when he double clicks the file, his program runs. Wouldn't this mean that the file type association is already made, and all that's left is to grab the filename off the command line?
Carson Myers
carson Myers is right
ecross
+1  A: 

Did you include code in your Main() to read the commandline parameter? e.g.

static void Main(string[] args)
{
    string fileToOpen = "";
    if (args.Length == 1)    
       fileToOpen = args[0];

   ...
}

If you have done this, then I guess you haven't properly registered your program to open this file type. Right-click any .ddd file, select Properties, and where it says "Opens with:" make sure your program is shown. If not, click Change and browse to your program.

Charles
Only if his program is registered to handle that file type. I think he wants to know how to do the that at install time
Joel Coehoorn
I get a red dotted line under args.Count == 1 and the next error:Operator '==' cannot be applied to operands of type 'method group' and 'int'
ecross
@ecross: sorry, it should have been args.Length.
Charles
+1  A: 

Are you looking to register the file extension in the registry?

48klocs
no, I already did this
ecross
A: 

I slitly changed charles m's post and this works fine:

string[] args = Environment.GetCommandLineArgs();
string fileToOpen = "";
if (args.Count() == 2)
{
    fileToOpen = args[1];
}

thanks for your suggestions, Ecross

ecross
args.Count() requires using System.Linq; I recommend args.Length instead.
Charles