try this code. This will save your file in the images folder and if the image file already exist then it wil rename it.
<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
<asp:Button ID="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"/>
protected void UploadButton1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Call a helper method routine to save the file.
// Specify the path to save the uploaded file to.
string savePath = Server.MapPath("images/");
// Get the name of the file to upload.
string fileName = FileUpload1.FileName;
// Create the path and file name to check for duplicates.
string pathToCheck = savePath + fileName;
// Create a temporary file name to use for checking duplicates.
string tempfileName = "";
// Check to see if a file already exists with the, same name as the file to upload.
if (File.Exists(pathToCheck))
{
int counter = 2;
while (File.Exists(pathToCheck))
{
// if a file with this name already exists,
// prefix the filename with a number.
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter++;
}
fileName = tempfileName;
// Notify the user that the file name was changed.
UploadStatusLabel.Text = "A file with the same name already exists." +
"<br />Your file was saved as " + fileName;
}
else
{
// Notify the user that the file was saved successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
// Append the name of the file to upload to the path.
savePath += fileName;
FileUpload1.SaveAs(savePath);
Image1.ImageUrl = "images/" + FileUpload1.FileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}