How to rename an image

2019-08-01 09:18发布

I have an image, person1.png, and four other images, person2.png, person3.png, person5.png, and person4.png. I want to rename these images in C# code. How would I do this?

4条回答
做自己的国王
2楼-- · 2019-08-01 09:45

On Windows Phone 7 the API methods to copy or move (rename) a file don't exist. (See http://msdn.microsoft.com/en-us/library/57z06scs(v=VS.95).aspx) You therefore have to do this yourself.

Something like:

var oldName = "file.old"; var newName = "file.new";

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var readStream = new IsolatedStorageFileStream(oldName, FileMode.Open, store))
    using (var writeStream = new IsolatedStorageFileStream(newName, FileMode.Create, store))
    using (var reader = new StreamReader(readStream))
    using (var writer = new StreamWriter(writeStream))
    {
        writer.Write(reader.ReadToEnd());
    }

    store.DeleteFile(oldName); 
}
查看更多
Bombasti
3楼-- · 2019-08-01 10:06

Since the PNG files are in your XAP, you can save them into your IsolatedStorage like this:

//make sure PNG_IMAGE is set as 'Content' build type
var pngStream= Application.GetResourceStream(new Uri(PNG_IMAGE, UriKind.Relative)).Stream;

int counter;
byte[] buffer = new byte[1024];
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
   using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(IMAGE_NAME, FileMode.Create, isf))
   {
      counter = 0;
      while (0 < (counter = pngStream.Read(buffer, 0, buffer.Length)))
      {
             isfs.Write(buffer, 0, counter);
      }    

      pngStream.Close();

    }
 }

Here you can save it to whatever file name you want by changing IMAGE_NAME.

To read it out again, you can do this:

byte[] streamData;    

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
     using (IsolatedStorageFileStream isfs = isf.OpenFile("image.png", FileMode.Open, FileAccess.Read))
     {
          streamData = new byte[isfs.Length];
          isfs.Read(streamData, 0, streamData.Length);              
     }
}

MemoryStream ms = new MemoryStream(streamData);
BitmapImage bmpImage= new BitmapImage();
bmpImage.SetSource(ms);
image1.Source = bmpImage; //image1 being your Image control
查看更多
霸刀☆藐视天下
4楼-- · 2019-08-01 10:07

When you upload image this function auto change the image name to Full date and return the full path where the image saved and with it new name.

 string path = upload_Image(FileUpload1, "~/images/");
 if (!path.Equals(""))
    {
        //use the path var..
    }

and this is the function

    string upload_Image(FileUpload fileupload, string ImageSavedPath)
{
    FileUpload fu = fileupload;
    string imagepath = "";
    if (fileupload.HasFile)
    {
        string filepath = Server.MapPath(ImageSavedPath);
        String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                try
                {
                    string s_newfilename = DateTime.Now.Year.ToString() +
                        DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileExtension;
                    fu.PostedFile.SaveAs(filepath + s_newfilename);

                    imagepath = ImageSavedPath + s_newfilename;
                }
                catch (Exception ex)
                {
                    return "";
                }

            }

        }

    }
    return imagepath;

}

if you need more help i'll try :)

查看更多
何必那么认真
5楼-- · 2019-08-01 10:12

Use the FileInfo.MoveTo method documented here. Moving a file to the same path but with a different name is how you rename files.

FileInfo fInfo = new FileInfo ("path\to\person1.png"); fInfo.MoveTo("path\to\newname.png")

If you need to manipulate paths, use the Path.Combine method documented here

查看更多
登录 后发表回答