473,414 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Console application switches for argument values?

Can you create your own switches or attributes for a console app to specify
specific arguments?

In other words, specify a /f to represent the "from date" and /t as "to
date"? if I need to supply both?

Thanks, Dave
Nov 4 '06 #1
2 7557
"Dave" <Da**@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.com...
Can you create your own switches or attributes for a console app to
specify
specific arguments?
Yes and no. You can get at the command line arguments by using the
"Main(string[] args)" version of the program Main method. However, beyond
that you're on your own. You'll need to parse the strings yourself to turn
them into something that's usable to control your program.

I think I read something in this newsgroup recently (or possibly
microsoft.public.dotnet.framework) about a third-party class that helps map
command line arguments to some class properties or something, but I don't
recall the specific. You can use Google Groups to get more details.

Pete
Nov 4 '06 #2
On Fri, 3 Nov 2006 19:52:01 -0800, Dave <Da**@discussions.microsoft.comwrote:
>Can you create your own switches or attributes for a console app to specify
specific arguments?

In other words, specify a /f to represent the "from date" and /t as "to
date"? if I need to supply both?

Thanks, Dave
Yes.

Here is the code...

You will need to
1. go to menu Project/YourProjectName Properties
2. On the DEBUG tab in the text box labeled "Command line arguments:" enter
/f 12/1/2006 /t 12/1/2007 (no quotes)
3. run the app

using System;
using System.Collections.Generic;

namespace TestConsole
{
class Program
{
static int Main(string[] args)
{
DateTime start = DateTime.Now; ;
DateTime end = DateTime.Now;

// for this command line:
// /f 12/1/2006 /t 12/1/2007
if (args[0] == @"/f" && args[2] == @"/t")
{

bool good = true;
if(! DateTime.TryParse(args[1], out start)){
Console.WriteLine("Invalid from date.");
good = false;
}

if (!DateTime.TryParse(args[3], out end))
{
Console.WriteLine("Invalid to date.");
good = false;
}
if (!good)
return 1;
}
else if (args[0] == @"/t" && args[2] == @"/f")
{
bool good = true;
if (!DateTime.TryParse(args[3], out start))
{
Console.WriteLine("Invalid from date.");
good = false;
}

if (!DateTime.TryParse(args[1], out end))
{
Console.WriteLine("Invalid to date.");
good = false;
}
if (!good)
return 1;
}
else
{
Console.WriteLine("Command line switches are not valid.");
}

Console.WriteLine("From = {0}, To = {1}", start, end);
Console.ReadLine();
return 0;
}
}
}

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Nov 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Job Lot | last post by:
How can I terminate console application in Try…Catch…Finally…End Try block so that code in Finally gets executed. If I use End statement Finally does not get executed. Following is my code...
6
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
12
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would...
13
by: kd | last post by:
Hi All, Would anybody be able to post me the command used in vb.net to execute an application from within the current application? Thanks. kd
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
4
by: dm1608 | last post by:
Hi -- I have a VB6 application that I plan on rewriting in C#.NET. The program today basically connects to a SQL Server and parses varies system stats from our mainframes and rolls the data up...
4
by: Mo | last post by:
Hi, I am writing a console application to send a key sequence to an old clunky application on a regular interval using the windows scheduler. I can get it to work if it is a windows form...
15
by: Michael C | last post by:
Is it possible to have an app that is both a console app and a windows app? If there are no command line switches then it will run as a console app, if there are command line switches then it will...
1
by: mquincey | last post by:
One of the features offered by .NET 2.0 is the use of the TraceSource class. In an attempt to demonstrate its use, I wanted to run my test under the following conditions: 1. Use TraceSource class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.