Tuesday 26 May 2015

Delete partitions on USB Flash Drive

Today, I was using my USB Flash Drive on my MAC and had to format from MAC and accidently created a partition of 200MB. This made only 200MB accessible on my Drive usable from my windows machine. Even from Disk Management tool on windows, I couldn't do much to claim back my 16GB.
Finally Diskpart tool in windows came for my help.

Follow the below steps to delete all the partition on your drive and get back the full space to use:
  1. Open Command Prompt in Administrative mode (elevated mode).
  2. In the prompt, Type Diskpart
  3. In the prompt, Type List disk
  4. Note the disk number that corresponds to your USB drive (it should be obvious going by size)
  5. select disk X where X is the number from step 4
  6. In the prompt, Type list partition - There should be two, numbered 0 and 1, each about 7 GB
  7. In the prompt, Type select partition 0
  8. In the prompt, Type delete partition
  9. In the prompt, Type select partition 1
  10. In the prompt, Type delete partition
  11. In the prompt, Type create partition primary
  12. In the prompt, Type exit
  13. Exit Command Prompt (type exit or just close the window)
  14. In Windows, go to Computer and try to open the disk. It will ask you to format it.
  15. Format it with the default settings and give it a name if you want.
  16. It should now a single, unified partitioned drive.

Sunday 3 May 2015

C# - WebClient Authentication not working

Today I had a scenario where I was using WebClient class to POST XML to a particular Url.

Even after many trials, the authentication was not working fine.

Finally I had to do it the old fashion way of specifying the authorization credentials in the header.

This scenario happens because WebClient doesn't send the Authorization header untill it receives a 401 status.

The code that failed:

string xml = @"<messages>......</messages> ;
string url = new UriBuilder("http", "www.abc.com", 9443).ToString();

// create a client object
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Credentials = new NetworkCredential("user1", "password1");
// performs an HTTP POST
string resp = client.UploadString(url, xml);
txtResponse.Text = "Response:" + resp;
}


The workaround:

string xml = @"<messages>......</messages> ;
string url = new UriBuilder("http", "www.abc.com", 9443).ToString();

// create a client object
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("user1:password1"));
// performs an HTTP POST
string resp = client.UploadString(url, xml);
txtResponse.Text = "Response:" + resp;
}