How can these be done. For example running a shourtcut thats path is this

C:\app.exe -sync

will run a certain part of the application(the sync function). Do i need to make a seperate application. Also say i have a program that plays music files. then i have a music file that i right click and select to open with ,my proggie. How could i make it so that it knows to play that file?

i hope thats clear. The main thing i would like to know is the command line arguments.

-T

Dani AI

Generated

Helpful clarification and practical steps following the suggestions from , and .

Important detail: the argument array that the Main entry point receives is the list of user-supplied tokens only; the raw command-line array available from the environment includes the executable path as element zero. That difference changes index offsets when code expects "first argument" vs. "executable name + args".

Windows behavior and shortcuts: Explorer passes the selected filename as a quoted command-line argument when using "Open with" or when a file association is registered. For permanent associations the shell open command must include the placeholder "%1" (use "%*" to accept multiple selected files). For shortcuts, the Target should contain the quoted application path (if it has spaces) followed by any switches and then the file placeholder; otherwise the shell will split incorrectly.

Parsing and robustness: do not rely on fixed positions. Treat tokens that begin with '-' or '/' as switches and treat other tokens as filenames; strip surrounding quotes before using paths; accept both single and multiple file inputs and validate each path exists before attempting playback. For debugging, log the exact argument list the process receives when launched from Explorer, a shortcut, and from the command prompt to confirm what is actually being passed.

Safety and tooling: validate and sanitize all incoming paths, handle exceptions gracefully, and avoid executing anything based solely on an unvalidated argument. For more complex option sets, prefer a small option-parsing library rather than ad hoc string splitting. These points extend the earlier replies and address the practical issues needed to implement a -sync switch and an "open with" file-playback scenario.

Recommended Answers

All 7 Replies

Try the Environment.GetCommandLineArgs() method

Try the Environment.GetCommandLineArgs() method

Ok, thanks i check it out.

-T

static void Main(string[] args)
{
}

make your Main look like this and "args" is a string array of all the args passed

static void Main(string[] args)
{
}

make your Main look like this and "args" is a string array of all the args passed

Hey, thanks ive searched the web and ound some examples...and i will continue to mess around with it.

-T

C# program accept arguments in the order of args[0], args[1] etc.

Console.WriteLine("Arguments-1 " + args[0]+" Argument-2 "+args[1]);

read full source code

chan.

What is commandline argument with example ?

commented: Don't bump old threads -1

Welcome sandeep9808.

Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.