防止文件重名方法

protected void btnUpload_Click(object sender, EventArgs e)
{
if (upload1.HasFile)
{
HttpPostedFile file
= upload1.PostedFile;
string extension = Path.GetExtension(file.FileName);
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + extension;
string path = Server.MapPath("uploads");
string savePath = Path.Combine(path, fileName);
//Response.Write(savePath);
string imagePath = "uploads/" + fileName;
file.SaveAs(savePath);
if (extension.Equals(".bmp") || extension.Equals(".jpg") || extension.Equals(".gif"))
{
Image1.ImageUrl
= imagePath;
}
}
}

1.重新命名成yyyyMMddhhmmss+后缀名的文件就行了。
2.Guid
3.yyyyMMddhhmmss+原文件名
原文地址:https://www.cnblogs.com/sig556/p/1576148.html