Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘command’

FTP Commands to upload file

Posted by Paul on October 30, 2010

Sometimes you might end up with a machine where the ftp tools are not accessible.  Here we can use the ftp.exe command line tool along with Windows Operating system.


Type ftp in the windows run command box.



In the appearing command line window type open (I am typing my site named

It will be prompting for the user name and password.



Type your username and press enter.  Wait for 2 seconds and you will be prompted for the password.  Type the correct password and press enter.  Wait for 2 seconds and if the password is valid you will be getting the message”User * logged in”.

Note: The password typed won’t be displayed on the screen.


If you can see the above message Congratulations!!! You have successfully logged in.

Now we can move to the next step

Step 4:

Type the command dir in the above screen.  Wait for 2 seconds.  You will be listed with the directories and files in the root folder.

Step 5:

Create a new directory named “fxlogics”.  For creating a new directory use command

mkdir fxlogics

Step 6:

Copy files to the new directory.

First we have to change the directory to fxlogics.  For this type the command

cd fxlogics

Step 7:

You can ensure you are in the correct directory by entering the dir command again.

Step 8: Upload File

Here you are uploading a file from the local computer to the ftp site’s current directory.

Use command put filepath

Alternatively you can type put and drag and drop the file from explorer to the command window.  So the exact path will be shown correctly.

Eg: put c:\readme.txt

Step 9:

Ensure the file is copied correctly by typing the dir command again.

If you can see the file in the command screen you have uploaded the file successfully.

Step 10:

Close the command window by typing the quit command.

More Commands

Command Description Example
help Shows the commands list help
quit Closes the ftp window quit
dir Displays the directory contents dir
cd Change directory cd mydir
mkdir Make Directory mkdir newdirname
put Uploads a file from the local file system. put “c:\program files\a.txt”
delete Deletes the file from the directory delete “the file name.exe”



You can use the up and down arrow while in the command window, to traverse between the commands typed.  It will save the time and effort to type the command again.

Hope you enjoyed the article 🙂


Posted in C# | Tagged: , , , | 2 Comments »

Command Pattern – Undo Example

Posted by Paul on October 18, 2010

Friends, here I am trying to show the usage of Command Design Pattern, one among 23 design patterns.

Personally I like this pattern very much, it is making use of good object oriented constructs and simplifies the logic of enabling Undo. 


You are working on a painting application.  For each brush stroke you need to provide the Undo feature.

The brushes presently available are square and rectangle. In future more and more brushes will be available.  Our challenge is to provide the user with Undo feature. Clearly, on clicking the undo button, the last stroke should be cleared from the canvas.


Command Pattern applied to the scenario

The definition of Command Pattern says “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations”

Command Pattern Explained

The pattern says that all the operations should be converted into objects, so that we log the objects.  In future we can call Do() or Undo() operations on the object.   The object should be smart enough to store the request parameters inside it to support the Do and Undo operations.

Command Pattern for our Scenario

Applied to our scenario, we have got two brushes:

  • Square Brush
  • Rectangle Brush

The other parameters would be Canvas which is the PictureBox control on which we draw and the X, Y location on mouse pointer on the canvas.   Formulating this we can create 2 classes:

SquareCommand class for square drawing operation and Rectangle class for rectangle drawing operation.

The Do() operation would be drawing a square for the square class and drawing a rectangle for the rectangle class.

We can formulate an interface called ICommand having the Do() and Undo() methods.

public interface ICommand


void Do();

void Undo();


The classes would be SquareCommand and RectangleCommand which implement s the above interface.

Architecture – Class Diagram



When the application is executed, the user will be having a blank white screen, with a set of colors to choose from.  The default brush selected will be Square and on clicking the canvas a new square will be drawn on the canvas.  The default color selected is Blue and is stored in the variable _activeColor.

The form level variables declared are:

private ShapeEnum _activeShape = ShapeEnum.Square;

private Color _activeColor = Color.Blue;

private Bitmap _bitmap;

private Graphics _graphics;

For each mouse down operation a new square class will be created.  That means if 10 clicks are made 10 square classes will be created and the Do() operation is called   An enumeration variable called _activeShape is used to keep track of the current shape selected by the user.  The mouse down event would look like the following.

private void Canvas_MouseDown(object sender, MouseEventArgs e)


// Each mouse down creates a new command class instance

ICommand command = null;

if (_activeShape == ShapeEnum.Square)

command = new SquareCommand(_bitmap, _activeColor, e.X, e.Y);

else if (_activeShape == ShapeEnum.Rectangle)

command = new RectangleCommand(_bitmap, _activeColor, e.X, e.Y);





Let us take an example: assume the user clicked the mouse at Point X=100 and Y=200.  As the _activeShape is Square,  a new SquareCommand class instance is created with arguments e.X and e.Y.  The remaining arguments are bitmap and the active color.  Then the command instance would be pushed to a stack and the Do() operation is called.

The stack provides a storage for usage of the command instance later.  The declaration of stack is:

private Stack<ICommand> _commandStack = new Stack<ICommand>();

Inside the SquareCommand Class

We can have a look on the Square class.  Both the SquareCommand and RectangleCommand class will be having same implementations except in the Do() operation of drawing the respective shape.

public class SquareCommand : ICommand


private Point _point;

private Bitmap _bitmap;

private Graphics _graphics;

private Color _color;

public SquareCommand(Bitmap bitmap, Color color, int x, int y)


_bitmap = bitmap;

_graphics = Graphics.FromImage(_bitmap);

_color = color;

_point = new Point(x, y);


public void Do()


// Save the current pixel colors for a future UNDO perform


// Do the drawing

_graphics.FillRectangle(new SolidBrush(_color),

new Rectangle(_point.X, _point.Y, Width, Height));


private const int Width = 50;

private const int Height = 50;

private IList<Color> _colors = new List<Color>();

private void SaveCurrentPixels()


for (int i = _point.X; i < _point.X + Width; i++)

for (int j = _point.Y; j < _point.Y + Height; j++)

_colors.Add(_bitmap.GetPixel(i, j));


/// <summary>

/// Perform Undo by restoring back the pixels to previous colors

/// </summary>

public void Undo()


int ix = 0;

for (int i = _point.X; i < _point.X + Width; i++)

for (int j = _point.Y; j < _point.Y + Height; j++)

_bitmap.SetPixel(i, j, _colors[ix++]);



The constructor is called with the bitmap and the x, y positions which are then stored into class fields.  When the Do() method is called the current pixel colors are saved into the _colors list.  This would enable us to perform the Undo() method later.  After that the graphics.FillRectangle() method is called to draw the square.  The width and height of the square would be set to 50 pixels using the constants Width and Height respectively.

The Undo() method just restores the previous pixel values using the bitmap.SetPixel method() by iterating through each pixel starting from x, y position.

Performing the Undo() on clicking Undo button

As we encapsulated each operation to a class with parameters and logged them into the command stack, it is now easier to call the Undo() method.  Please remind that on clicking the Undo button we have to get the last operation instance and call the Undo() method of it, and removing it from the stack.

The code for it is:

private void UndoButton_Click(object sender, EventArgs e)


// Check command stack contains items

if (_commandStack.Count > 0)


// Remove the last command

ICommand lastCommand = _commandStack.Pop();

// Call the Undo method





First, the command stack count is checked to ensure there are commands inside it.  Then the last command is popped out using the Pop() method.  It will give us the last instance in the stack as well as removes it.  Then, the Undo() operation is invoked. After, the RefreshUI() method is called to update the UI with the changes.


The command pattern provides an object oriented manner to provide Undo feature to our application in a flexible manner.

But the creation of lot of object instances is one of the draw back of this method which usually people say about.  I would say that in the other hand, the command pattern is giving us the advantage of keeping the code more manageable.

Source Code

I am happy to let you know that the source code for the article is available at


Posted in C#, Design Patterns | Tagged: , , , , | Leave a Comment »