Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I have an Item Form Master where i am making an Item and here i have given a functionality of uploading an image for the item on a popup window and save it in a folder with the same name as image name.

Now when i am saving the item then i want to rename the name of the file uploaded but i am not able to rename the same.

The code with which i am trying to save the same is:-

C#
if (!File.Exists(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName)))
            {
                string newImgName = Convert.ToString(itemID);

                FileUpload file = new FileUpload();

                file.SaveAs(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + file.FileName.Replace(itemImgName + "." + fileExt, newImgName + "." + fileExt)));
            }


The above code is executing successfully but the image name doesn't get renamed as per my requirement.

Please let me know where i am going wrong or there is some other option of doing the same.
Posted

1 solution

try by saving new file and deleting the old file..

C#
if (!File.Exists(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName)))
            {
                string newImgName = Convert.ToString(itemID);
 
                FileUpload file = new FileUpload();
 
                file.SaveAs(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + newImgName + "." + fileExt)));

                FileInfo MyFile = new FileInfo(Server.MapPath("~/ItemImage/" + Convert.ToString(Request.QueryString["compid"]) + "/" + Convert.ToString(Request.QueryString["offID"]) + "/" + itemImgName + "." + fileExt));
          
MyFile.Delete();
            }
 
Share this answer
 
v3
Comments
Varun Sareen 8-Dec-11 2:27am    
Dear Karthik,

Your code is bit confusing to me as you are actually deleting the new file and i am not able to see that my file is being renamed.
Karthik Harve 8-Dec-11 2:38am    
I have Updated..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900