Split Commandline Arguments/parameters Into Variables

Posted on 2012-08-30
Last Modified: 2012-08-30
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()

        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

Question by:Daniel_P67
    LVL 74

    Accepted Solution

    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.
    LVL 1

    Author Comment

    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

    LVL 1

    Author Closing Comment

    Explanation was good enough to help me solve the issue. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Suggested Solutions

    Title # Comments Views Activity
    c# vs2013 corrupt .dll 4 34
    How to deploy LocalDB to SQL Server 2012 4 29
    Path of Workbook 3 30
    Handling arrays 14 28
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now