Setting up the project

Table of contents | Next: Defining the application options

To get started, create a new C# Console Application in Visual Studio and call it Backup.

The default code for the Console application will be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Backup
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Let's clean this up a bit and modify it to be used with ConsoleFx:

using System;
using System.Collections.Generic;

namespace Backup
{
    internal static class Program
    {
        private static int Main()
        {
            return 0;
        }
    }
}
  • Remove the namespaces System.Linq and System.Text; we won't need them for this walkthrough.
  • Make the Program class internal and static... not important, but nice to do anyway.
  • Remove the string[] args parameter from the Main method. ConsoleFx doesn't need it to get the command-line arguments.
  • Change the Main method to return an int. Most CLI applications return an integer value (with zero representing success) so that they can be handled from calling batch files.

Next, add a reference to the ConsoleFx assembly, and add the following namespace imports:

using ConsoleFx;
using ConsoleFx.Programs.Simple;
using ConsoleFx.Validators;
  • ConsoleFx.Programs.Simple denotes that we want to create a simple console application with only a single functionality. By using this namespace, the more advanced features of ConsoleFx are hidden from you. To be able to create a complex console application, you would use a different namespace - ConsoleFx.Programs.Complex - which is discussed in the another Getting Started walk-through.
  • ConsoleFx.Validators contains the classes that we'll need to validate the data being passed in the command-line arguments.

Next, to get the ConsoleProgram class wired up into your code, replace the code in the Main method with the following:

var app = new ConsoleProgram(Handler);
try
{
    return app.Run();
}
catch (Exception ex)
{
    return app.HandleException(ex);
}

Also, add a new static method called Handler. This will be the method that performs the actual backup functionality, but we'll leave it blank for now and just return a success code.
private static int Handler()
{
    return 0;
}

Your code at this point

using System;
using System.Collections.Generic;

using ConsoleFx;
using ConsoleFx.Programs.Simple;
using ConsoleFx.Validators;

namespace Backup
{
    internal static class Program
    {
        private static int Main()
        {
            var app = new ConsoleProgram(Handler);
            try
            {
                return app.Run();
            }
            catch (Exception ex)
            {
                return app.HandleException(ex);
            }
        }

        private static int Handler()
        {
            return 0;
        }
    }
}

Table of contents | Next: Defining the application options

Last edited Nov 26, 2011 at 8:22 AM by jeevanjj, version 3

Comments

No comments yet.