Renaming file in C# on Server I am using the following code to rename the uploaded files for any future name clashes. Please tell if my approach is right or I should follow some other approach or approach which is better. 

[HttpPost]
    public ActionResult Upload(HttpPostedFileBase fileData)
    {
        if (fileData != null &amp;&amp; fileData.ContentLength &gt; 0)
        {
            //var fileName = Server.MapPath("~/Content/Images/" + Path.GetFileName(fileData.FileName));
            int pictureCount = 800000;
            pictureCount += db.Pictures.Count();
            string extension = Path.GetExtension(fileData.FileName);
            string renamedImage = Server.MapPath("~/Content/Images/Categories/cat" + pictureCount + extension);
            fileData.SaveAs(renamedImage);
            return Json(Path.GetFileName(renamedImage));
        }
        return Json(false);
    }
If you have two simultaneous file uploads db.Pictures.Count(); will return same number. So you will get a name clash.

If db.Pictures is a database then you can create and auto increment identity field and use it to save the file.

If there is no option with database you can use two more approaches:
Use GUID instead of picture name

[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
    if (fileData != null &amp;&amp; fileData.ContentLength &gt; 0)
    {
        string extension = Path.GetExtension(fileData.FileName);

        string renamedImage = null;
        do
        {
            renamedImage = Server.MapPath("~/Content/Images/Categories/cat" + System.Guid.NewGuid().ToString("N") + extension);

        } while( /*verify that renamedImage file does not exist*/ );

        fileData.SaveAs(renamedImage);
        return Json(Path.GetFileName(renamedImage));
    }
    return Json(false);
}


The drawback of this approach is that it is possible to upload same pictures to the server.

To mitigate that you can try using hash of the image as a file name. Before writing file contents to the disk check if that file already exists. Note: the check and file save operation must be made in thread-safe manner.