Tuesday, October 29, 2013

The process cannot access the file because it is being used by another process


Problem-


The process cannot access the file because it is being used by another process

This is common problem, when we create file using File.Create() method.




Theprocesscannotaccess


Reason-


When we use File.Create() method to create file then the object of FileStream class is created and no other process can use or access the created file until the file is closed.

Solution -

This problem can be easily resolved by modifying your code like this-

File.Create (@"c:\Text.txt").Close();

Means use Close () with File.Create () method. It will create the file and after that it will close the related file.



Tuesday, October 1, 2013

How to make DatePicker control as null or empty in Windows Application?


This is common problem with DatePicker control - We cannot assign null or empty value to its value property. In this case, showing DatePicker control as empty is bit difficult. I have done one work around for this and now I am able to show empty DatePicker control.

In this article, I will explain - How to make DatePicker control as null or empty?

Check the below code implementation.

Workaround-

To make it possible, I have used "CustomFormat" property of DatePicker control.

//To set DatePicker Control format as "MM/yyyy"
private void dtPickerDOB_ValueChanged(object sender, EventArgs e)
{
   dtPickerDOB.Format = DateTimePickerFormat.Custom;
   dtPickerDOB.CustomFormat = "MM/yyyy";
}


In above code, I am setting CustomFormat = "MM/yyyy". So the date will be displayed like this- "10/2013"

Now to make this DatePicker cotrol as empty-

//To make DatePicker Control as empty
private void btnClearDate_Click(object sender, EventArgs e)
{
  // Setting CustomFormat as Space.           
  dtPickerDOB.Format = DateTimePickerFormat.Custom;
  dtPickerDOB.CustomFormat = " ";




In above code, I have set CustomFormat = " " so empty value will be displayed in DatePicker control.

Try this code


Thanks