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

Posts Tagged ‘file’

Windows Azure – BlockBlob PutBlock Method

Posted by Jean Paul on November 30, 2011

In this article we are discussing about uploading blob using PutBlock method. I would like to give a note that there are two types of blob in Windows Azure:

· Block Blob

· Page Blob

The Block Blob uploading is discussed here as Block Blobs are used for large file uploads. The associated code was developed to upload large files to Windows Azure Blob. The large file will be splitted into 4 MB chunks and then uploaded.


Following are the important methods used:

· CloudBlockBlob.PutBlock()

· CloudBlockBlob.PutBlockList()

The PutBlock method is called for each 4MB file chunk and PutBlockList method is called at the end passing the file chunk ids.


Following is the code of AzureBlobUtil.cs

public class AzureBlobUtil


private const int MaxBlockSize = 4000000; // Approx. 4MB chunk size

public Uri UploadBlob(string filePath, CloudStorageAccount account, string containerName)


byte[] fileContent = File.ReadAllBytes(filePath);

string blobName = Path.GetFileName(filePath);

return UploadBlob(fileContent, account, containerName, blobName);


public Uri UploadBlob(byte[] fileContent, CloudStorageAccount account, string containerName, string blobName)


CloudBlobClient blobclient = account.CreateCloudBlobClient();

CloudBlobContainer container = blobclient.GetContainerReference(containerName);


CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

HashSet<string> blocklist = new HashSet<string>();

foreach (FileBlock block in GetFileBlocks(fileContent))




new MemoryStream(block.Content, true),






return blob.Uri;


private IEnumerable<FileBlock> GetFileBlocks(byte[] fileContent)


HashSet<FileBlock> hashSet = new HashSet<FileBlock>();

if (fileContent.Length == 0)

return new HashSet<FileBlock>();

int blockId = 0;

int ix = 0;

int currentBlockSize = MaxBlockSize;

while (currentBlockSize == MaxBlockSize)


if ((ix + currentBlockSize) > fileContent.Length)

currentBlockSize = fileContent.Length - ix;

byte[] chunk = new byte[currentBlockSize];

Array.Copy(fileContent, ix, chunk, 0, currentBlockSize);


new FileBlock()


Content = chunk,

Id = Convert.ToBase64String(System.BitConverter.GetBytes(blockId))


ix += currentBlockSize;



return hashSet;


public Stream DownloadBlobAsStream(CloudStorageAccount account, string blobUri)


Stream stream = new MemoryStream();

CloudBlobClient blobclient = account.CreateCloudBlobClient();

CloudBlockBlob blob = blobclient.GetBlockBlobReference(blobUri);

if (blob != null)


return stream;


public string DownloadBlobAsFile(CloudStorageAccount account, string blobUri)


Stream stream = DownloadBlobAsStream(account, blobUri);

string fileName = "Blob.file"; // Please change file name based on your need

if (stream != null)


FileStream fileStream = File.Create(fileName);

stream.Position = 0;



return fileName;


return string.Empty;



internal class FileBlock


public string Id





public byte[] Content






Testing Code

For uploading blob into the container use the following code:

// Upload

Uri blobUri = _blobUtil.UploadBlob(




// Download

Stream stream = _blobUtil.DownloadBlobAsStream(




Associated Example

On running the example, after opening and uploading an image file, you can see the following result. The first box shows the source image. The second box shows the uploaded image from Azure.



Please ensure the Storage Emulator is running while testing the example


You can find more information on Block and Page blob using following link:


In this article we have seen the usage of Block Blob upload as chunks. The source code can be used to upload large files to Azure Blob. The associated example can be downloaded along with this article. The utility method was verified with 100MB file upload and download.

Posted in Azure | Tagged: , , , , , , , , , | 2 Comments »

How to edit app.config file?

Posted by Jean Paul on January 13, 2011

The application configuration file provides flexibility in reading application settings.  But once in a while it is needed to change the settings programmatically. 

We can see how it is made possible.

Step 1: Create new Windows Forms application

Create a new windows forms application and add a config file into it.

Step 2: Add reference to System.Configuration

Add reference to System.Configuration namespace

Step 3: Add code to edit config

System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            configuration.AppSettings.Settings.Add(“font”, “Calibri”);



For editing an existing appSettings key – we have deleted and added the key.

Posted in WinForms | Tagged: , , , , | Leave a Comment »

FTP Commands to upload file

Posted by Jean 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 »