PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 19, 2022

[FIXED] How to get command line parameters and put them into variables?

 May 19, 2022     c#, parameters, variables     No comments   

Issue

I am trying to make an application. Can someone help me how to get command line parameters and put them into variables/strings. I need to do this on C#, and it must be 5 parameters.

The first parameter needs to be put into Title variable. The second parameter needs to be put into Line1 variable. The third parameter needs to be put into Line2 variable. The fourth parameter needs to be put into Line3 variable. And the fifth parameter needs to be put into Line4 variable.

Tank You For Helping!

Edit:

I need to add this into Windows Forms Application.


Solution

You can do it in one of two ways.

The first way is to use string[] args and pass that from Main to your Form, like so:

// Program.cs
namespace MyNamespace
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyForm(args));
        }
    }
}

And then in MyForm.cs do the following:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm(string[] args)
        {
            if (args.Length == 5)
            {
                Title = args[0];
                Line1 = args[1];
                Line2 = args[2];
                Line3 = args[3];
                Line4 = args[4];
            }
        }
    }
}

The other way is to use Environment.GetCommandLineArgs(), like so:

// MyForm.cs
namespace MyNamespace
{
    public partial class MyForm : Form
    {
        string Title, Line1, Line2, Line3, Line4;
        public MyForm()
        {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length == 6)
            {
                // note that args[0] is the path of the executable
                Title = args[1];
                Line1 = args[2];
                Line2 = args[3];
                Line3 = args[4];
                Line4 = args[5];
            }
        }
    }
}

and you would just leave Program.cs how it was originally, without the string[] args.



Answered By - Jashaszun
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing