Thursday, 5 April 2018

What is the best way for read a large file into a byte array using c#

Description : In this post how to convert a file stream in a byte array using c#. this code is use for convert a file into byte array and also send this byte array into FTP or Other file storage code. in my early post of upload file in FTP using c# use this code for convert any file from stream or file path to byte array

- Below code for convert file stream to byte array from specific file path using c#

public byte[] ReadAllBytes(string fileName)
{
    byte[] buffer = null;
    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        buffer = new byte[fs.Length];
        fs.Read(buffer, 0, (int)fs.Length);

    or

    using (var reader = new BinaryReader(filestream))
    {
        byte[] file = reader.ReadBytes((int)filestream.Length);
        }
    }
    return buffer;
}

Convert byte array to string and string to byte array using c#

Description : In this post how to convert a simple string into byte array or convert a byte array to string using c#. this code is use in my early post for Download file from FTP.

- Below code for convert string to byte array using c#

string stringToConvert = "Hello World !";

byte[] buffer = Encoding.UTF8.GetBytes(stringToConvert);

- Below code for convert byte array to string using c#

string converted = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

Tuesday, 3 April 2018

How to remove queue from azure queue using c#

Description : In this post how to remove queue from azure using c#. here same steps follow in my previous post for how to add message in azure using c#. and also check how to retrive queue from azure using c#

Step 1 : Downlod below NuGet package for add message using c#. Install both package one by one in your project use NuGet console or NuGet package managerr

- WindowsAzure.Storage
- WindowsAzure.ConfigurationManager

Step 2 : Get your connection string from Azure. Go to Azure Portal
Go to Storage Account -> Click on your storage account -> Inside Setting Click on Access keys -> In this window select Copy connection string. your connection string look like below

- DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey

Step 3 : Write below code for remove message from queue using c#

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey"

CloudStorageAccount account = CloudStorageAccount.Parse(ConnectionString); // Cloud Storage Account

CloudQueueClient queueClient = account.CreateCloudQueueClient(); // Queue Client Create

CloudQueue messageQueue = queueClient.GetQueueReference("YourAzureQueueName"); // Get Queue Reference

// this line for remove message from azure queue
messageQueue.DeleteMessage(MessageID, PopReceipt, null, null); // here message id is queue is get from azure and PopReceipt also you can get from queue

How to retrive queue from azure using c#

Description : In this post how to retrive queue message from azure using c#. here same steps for get message in my previous post of how to get message from azure using c#

Step 1 : Downlod below NuGet package for add message using c#. Install both package one by one in your project use NuGet console or NuGet package managerr

- WindowsAzure.Storage
- WindowsAzure.ConfigurationManager

Step 2 : Get your connection string from Azure. Go to Azure Portal
Go to Storage Account -> Click on your storage account -> Inside Setting Click on Access keys -> In this window select Copy connection string. your connection string look like below

- DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey

Step 3 : Create class for add json message in azure queue using c#

public class ResponseClass
{
    public string id { get; set; }
    public string name { get; set; }
}

Step 3 : Write below code for retrive queue from azure using c#

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey"

CloudStorageAccount account = CloudStorageAccount.Parse(ConnectionString); // Cloud Storage Account

CloudQueueClient queueClient = account.CreateCloudQueueClient(); // Queue Client Create

CloudQueue messageQueue = queueClient.GetQueueReference("YourAzureQueueName"); // Get Queue Reference

// this line for retrive 10 messages from azure queue
List<CloudQueueMessage> retrievedMessage = messageQueue.GetMessages(10).ToList();

using for loop one by one get queue response like below code

for (int i = 0; i < retrievedMessage.Count; i++)
{
    string Message = retrievedMessage[i].AsString;
    string ID = retrievedMessage[i].Id;
    string PopReceipt = retrievedMessage[i].PopReceipt; // this PopReceipt use for delete queue

    JsonResponse obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseClass>(Message);
}

How to add message in azure queue using C#

Description : In this post how to add message in azure queue storage using c# code. This code use in any .Net application like Winform application or MVC or ASP.Net. below steps for how to successfully add message in azure queue using c#

Step 1 : Downlod below NuGet package for add message using c#. Install both package one by one in your project use NuGet console or NuGet package managerr

- WindowsAzure.Storage
- WindowsAzure.ConfigurationManager


Step 2 : Get your connection string from Azure. Go to Azure Portal
Go to Storage Account -> Click on your storage account -> Inside Setting Click on Access keys -> In this window select Copy connection string. your connection string look like below

- DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey

Step 3 : Create class for add json message in azure queue using c#

public class RequestClass
{
    public string id { get; set; }
    public string name { get; set; }

    public RequestClass() { }

    public RequestClass(string _id, string _name)
    {
        this.id = _id; this.name = _name;
    }
}

Step 4 : Write below code for add message in your queue using C#.

string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=YourStorageAccountName;AccountKey=YourStorageAccountKey"

CloudStorageAccount account = CloudStorageAccount.Parse(ConnectionString); // Cloud Storage Account

CloudQueueClient queueClient = account.CreateCloudQueueClient(); // Queue Client Create

CloudQueue messageQueue = queueClient.GetQueueReference("YourAzureQueueName"); // Get Queue Reference

string MessageToAdd = JsonConvert.SerializeObject(new RequestClass(GUID, Name)); // Serialize JSON for add in queue

CloudQueueMessage m = new CloudQueueMessage(MessageToAdd); // Generate Cloud Message

messageQueue.AddMessage(m); // Finally add message in queue