Jayne M
Jayne M

Reputation:

Frustrated trying to read a path from an argument in C#

I'm passing /file:c:\myfile.doc and I'm getting back "/file:c:\myfile.doc" instead of "C:\myfile.doc", could someone please advise where I am going wrong?

            if (entry.ToUpper().IndexOf("FILE") != -1)
            {
                //override default log location
                MyFileLocation = entry.Split(new char[] {'='})[1];
            }

Upvotes: 1

Views: 599

Answers (6)

Carl
Carl

Reputation: 5991

Not an answer as I think it's been answered well enough already, but as you stated that you're a beginner I thought that I would point out that:

entry.split(new char[]{':'});

can be:

entry.split(':');

This uses:

split(params char[] separator);

This can be deceiving for new C# programmers as the params keyword means that you can actually pass in 1 to many chars, as in:

entry.split(':','.',' ');

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881273

The code you've posted would require the argument /file=c:\myfile.doc.

Either use that as the parameter or split on the colon (:) instead of equals (=).

Upvotes: 0

Scott Dorman
Scott Dorman

Reputation: 42516

The easiest way to do this is to just take a substring. Since you are reading this from the command line, the "/file:" portion will always be consistent.

entry.Substring(6);

This will return everything after the "/file:".

Upvotes: 2

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827256

Here is a good example of a command line argument parser.

Upvotes: 0

Craig
Craig

Reputation: 12012

You could also just lop off the 'file:' part. It is clearly defined and will be constant so it isn't THAT bad. Not great, but not horrible.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

You are splitting on "=" instead of ":"

Try

    if (entry.ToUpper().IndexOf("FILE:") == 0)
    {
         //override default log location
         MyFileLocation location = entry.Split(new char[] {':'},2)[1];
    }

Upvotes: 6

Related Questions