Click here to Skip to main content
15,894,630 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Friends,
I will retrieve the file from a appropriate folder. i can able to get the image name . but i can't rename that image file . Please Help me

C#
namespace webshoppe
{
    public partial class Image : System.Web.UI.Page
    {
        private static DirectoryInfo dir;
        private static FileInfo[] images;
        private static int currIndex = 0;
        private static int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
         dir = new DirectoryInfo(@"D:\Image");
            
           images = dir.GetFiles("*.jpg");
          
           
            count = images.Length;
            if (!IsPostBack)
            {
                if (count > 0)
                {
                    string strImageurl = images[currIndex].Name;
                    strImageurl = "~/Image/" + strImageurl;
                    Image1.ImageUrl = strImageurl;
                   
                    currentname();
                }
            }
        }
      
       
        public void currentname()
        {

            string strImageurl = images[currIndex].Name;
            lblimage.Text = strImageurl;
             images[currIndex].Name= txt_imagename.Text;
           
        }
        protected void submitbtn_Click(object sender, EventArgs e)
        {
            if (count > 0)
            {
                if (currIndex == (count - 1))
                {
                    currIndex = 0;
                }
                else
                {
                    currIndex++;
                }
                string newUrl = images[currIndex].Name;
                newUrl = "~/Image/" + newUrl;
                Image1.ImageUrl = newUrl;
                currentname();
            }
        }
    }
}


Error:
C#
'System.IO.FileSystemInfo.Name' cannot be assigned to -- it is read only
Posted

C# doesn't provide rename facility directly. But you can use either of these two methods.

1)
File.Copy(OldFileName, NewFileName);
File.Delete(OldFileName);
2)
File.Move(OldFileName,NewFileName);
 
Share this answer
 
C#
System.IO.File.Move(oldname, newname);
 
Share this answer
 
Comments
P.Vinoth 17-Aug-12 4:13am    
Its not working friend.
error:
Empty file name is not legal.
Parameter name: destFileName

I just want to rename the single image file.. please help me.
StianSandberg 17-Aug-12 4:28am    
You have to send in two valid filenames! You get the "Empty file name is not legal error" because either oldname or newname is empty.
System.IO.File.Move("D:\\myFile.txt", "D:\\myRenamedFile.txt"); works perfect..
P.Vinoth 17-Aug-12 5:56am    
Its working for .txt files friend.
But i need to rename the image file... Please help me.

Once again thanks fr your posting friend........
StianSandberg 17-Aug-12 6:20am    
System.IO.File.Move("D:\\myFile.jpg", "D:\\myRenamedFile.jpg"); ??
Do you get any exception when you try to rename?
P.Vinoth 18-Aug-12 3:08am    
no its not working friend..
C#
public bool Rename(string FileNameAndPath, string NewName)
 {
     bool ret = false;
    System.IO.FileInfo fi = new System.IO.FileInfo(FileNameAndPath);
    if (!fi.Exists)
        return ret;

    string NewFilePathName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(FileNameAndPath), NewName);
    System.IO.FileInfo f2 = new System.IO.FileInfo(NewFilePathName);
    try
    {
        if (f2.Exists)
        {
            f2.Attributes = System.IO.FileAttributes.Normal;
            f2.Delete();
        }

        fi.CopyTo(NewFilePathName);
        fi.Delete();
        ret = true;
    }
    catch
    {

    }

    return ret;
 }
 
Share this answer
 

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