2011-06-05 71 views
0

我有一个图像,person1.png和四个其他图像,person2.png,person3.png,person5.pngperson4.png。我想用C#代码重命名这些图像。我将如何做到这一点?如何重命名图像

+3

我认为这些都是在独立存储?您无法从代码中编辑XAP中的任何文件。 – keyboardP 2011-06-05 16:19:40

+0

其实,他们在XAP。我将如何将它们复制到IsolatedStorage? – JavaAndCSharp 2011-06-05 21:16:18

+1

我已经用一些代码回答了。 – keyboardP 2011-06-05 21:38:21

回答

2

由于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 
+0

这给了我一个NullReferenceException。我用我的图像名称(用引号)替换了PNG_IMAGE,即“/Images/Person1.png”。 – JavaAndCSharp 2011-06-06 00:15:41

+1

抛出了哪个'NullReferenceException'?您是否将阅读代码中的“image.png”更改为“/Images/Person1.png”? – keyboardP 2011-06-06 00:22:30

+0

它被引发到第一个代码片段的第一行。 – JavaAndCSharp 2011-06-06 00:27:34

2

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

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

如果你需要操作的路径,使用Path.Combine方法记录here

+0

虽然这可以在WP7环境之外工作,但不能在运行时执行此操作,因为WP7不支持这些方法。 – keyboardP 2011-06-05 16:37:13

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

当我尝试这个时,调试器说“IsolatedStorageException未处理”。我不知道为什么。 – JavaAndCSharp 2011-06-06 00:17:32

+0

这将允许您重命名IsolatedStorage中存在的文件,而不是从xap复制到独立存储,您的注释现在指出这是您的实际问题。 – 2011-06-06 07:35:06

0

当您上传图片的功能,自动改变图像的名称为Full日期并返回所在的完整路径的图像保存并与它新名字。

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; 

} 

,如果你需要更多的帮助,我会试试:)

+0

嗨艾哈迈德它更好地给予一些解释给定的代码...会变得更有帮助:) – 2013-01-17 14:40:36