Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:16:18+00:00 2026-05-30T00:16:18+00:00

I’m looking for a way that I can parse command line arguments into my

  • 0

I’m looking for a way that I can parse command line arguments into my WPF application with just a way of reading the value of the argument that the user passed.

As an example

application.exe /setTime 5

is there a way for me to have some code where I can just say:

MessageBox.Show(arg("setTime"));

Which will output 5

Working Solution

How to create smart WPF Command Line Arguments

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:16:19+00:00Added an answer on May 30, 2026 at 12:16 am

    The way I always do it is to specify the arguments as a "name"/"value" pair e.g.

    myprogram.exe -arg1 value1 -arg2 value2
    

    This means that when you parse the command line you can put the argument/value pairs in a Dictionary with the argument as the key. Then your arg("SetTime") will become:

    MessageBox.Show(dictionary["SetTime"]);
    

    (Obviously you don’t want the actual dictionary to be public.)

    To get the arguments in the first place you can use:

    string[] args = Environment.GetCommandLineArgs();
    

    This will return all the arguments so you will need to parse the array in steps of two (after first checking that the length is a multiple of two + 1):

    The first element of the array is the name of the executing program – MS Learn – so your loop needs to start from one:

    for (int index = 1; index < args.Length; index += 2)
    {
         dictionary.Add(args[index], args[index+1]);
    }
    

    This loops in steps of two as you define each argument is a pair of values: the identifier and the actual value itself, e.g.

    my.exe -arg1 value1 -arg2 value2
    

    Then you can simply see if the argument is specified by seeing if the key -arg1 is in the dictionary and then read it’s value:

    string value;
    if (dictionary.TryGetValue(arg, out value))
    {
        // Do what ever with the value
    }
    

    This means you can have the arguments in any order and omit any arguments you don’t want to specify.

    The only drawback with this method is if you have a flag like -debug (for example) which could be logically implemented with the presence or absence of the flag will need to be specified as -debug true (or 1 or on), but it does simplify things if you have flags that do require values (like configuration file paths, database connection strings etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.