如何重命名图像

时间:2011-06-05 16:17:58

标签: c# visual-studio visual-studio-2010 windows-phone-7

我有一张图片person1.png和另外四张图片,person2.pngperson3.pngperson5.pngperson4.png。我想用C#代码重命名这些图像。我该怎么做?

4 个答案:

答案 0 :(得分:3)

由于PNG文件位于您的XAP中,您可以将它们保存到IsolatedStorage中,如下所示:

//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();

    }
 }

您可以在此处通过更改IMAGE_NAME将其保存为您想要的任何文件名。

要再次阅读,您可以这样做:

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

答案 1 :(得分:2)

使用文档here记录的FileInfo.MoveTo方法。将文件移动到相同路径但名称不同的方法是重命名文件。

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

如果您需要操纵路径,请使用记录为here

的Path.Combine方法

答案 2 :(得分:1)

在Windows Phone 7上,不存在复制或移动(重命名)文件的API方法。 (见http://msdn.microsoft.com/en-us/library/57z06scs(v=VS.95).aspx)因此,您必须自己做。

类似的东西:

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); 
}

答案 3 :(得分:0)

上传图片时,此功能会自动将图片名称更改为完整日期,并返回保存图片的完整路径及其新名称。

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

这是函数

    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;

}

如果您需要更多帮助,我会尝试:)