• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

Split Commandline Arguments/parameters Into Variables

Stuck on a bit of code. We are calling a program with parameters, Example: program.exe \\server\file nameoffile pdf status. Want to take each parameter separated by a space and assign it to a string variable. On this line  "string[] words = args.Split(' ');", "args" is giving me the error system.array has no definition for Split. Have not been able to get past this point.

Any help or comments are welcomed!


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Programname : Form
    {
        
       
        string[] args = Environment.GetCommandLineArgs();
    
              
        public Programname()
        {
            InitializeComponent();
        }

       
        private void OpenDrawing_Click(object sender, EventArgs e)
        {


            string[] words = args.Split(' ');

            string docPath = words[0];
            string docFile = words[1];
            string docExt = words[2];
            string docType = words[3];
            string docStatus = words[4];

}
}
}

Open in new window

0
Daniel_P67
Asked:
Daniel_P67
  • 2
1 Solution
 
käµfm³d 👽Commented:
Exactly. args is already a string array, which is what Split would have given you back had args simply a string (rather than an array of strings). Just index args as the system will handle the splitting of the command line for you.
0
 
Daniel_P67Author Commented:
Great thanks!!! Checked with the below code. Placed a quick text box and pasted in the info out of order, worked just like i wanted.


  
//string[] words = args.Split(' '); //deleted

            string docPath = args[0];
            string docFile = args[1];
            string docExt = args[2];
            string docType = args[3];
            string docStatus = args[4];


            textBox1.Text = docPath + docExt + docFile;

Open in new window

0
 
Daniel_P67Author Commented:
Explanation was good enough to help me solve the issue. Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now