C# 如何重命名图像

C# 如何重命名图像,c#,visual-studio,visual-studio-2010,windows-phone-7,C#,Visual Studio,Visual Studio 2010,Windows Phone 7,我有一张图片,person1.png,还有另外四张图片,person2.png,person3.png,person5.png,和person4.png。我想用C代码重命名这些图像。我该如何做到这一点?使用FileInfo.MoveTo方法。将文件移动到同一路径但使用不同的名称是重命名文件的方式 FileInfo fInfo=newfileinfo(“path\to\person1.png”); fInfo.MoveTo(“path\to\newname.png”) 如果需要操作路径,请使用

我有一张图片,
person1.png
,还有另外四张图片,
person2.png
person3.png
person5.png
,和
person4.png
。我想用C代码重命名这些图像。我该如何做到这一点?

使用FileInfo.MoveTo方法。将文件移动到同一路径但使用不同的名称是重命名文件的方式


FileInfo fInfo=newfileinfo(“path\to\person1.png”);
fInfo.MoveTo(“path\to\newname.png”)


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

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


FileInfo fInfo=newfileinfo(“path\to\person1.png”);
fInfo.MoveTo(“path\to\newname.png”)


如果需要操作路径,请使用Windows Phone 7上记录的Path.Combine方法。用于复制或移动(重命名)文件的API方法不存在。(见)因此,你必须自己做这件事

比如:

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

在Windows Phone 7上,复制或移动(重命名)文件的API方法不存在。(见)因此,你必须自己做这件事

比如:

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

由于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();

    }
 }

由于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();

    }
 }

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

 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;

}
string upload\u Image(FileUpload FileUpload,string ImageSavedPath)
{
FileUpload fu=FileUpload;
字符串imagepath=“”;
if(fileupload.HasFile)
{
字符串filepath=Server.MapPath(ImageSavedPath);
String fileExtension=System.IO.Path.GetExtension(fu.FileName.ToLower();
字符串[]allowedExtensions={.gif“,.png“,.jpeg“,.jpg”};
for(int i=0;i

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

当您上载图像时,此功能会自动将图像名称更改为完整日期,并返回图像保存的完整路径和新名称

 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;

}
string upload\u Image(FileUpload FileUpload,string ImageSavedPath)
{
FileUpload fu=FileUpload;
字符串imagepath=“”;
if(fileupload.HasFile)
{
字符串filepath=Server.MapPath(ImageSavedPath);
String fileExtension=System.IO.Path.GetExtension(fu.FileName.ToLower();
字符串[]allowedExtensions={.gif“,.png“,.jpeg“,.jpg”};
for(int i=0;i


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

我想这些是在隔离存储中?您不能从代码中编辑XAP中的任何文件。实际上,它们在XAP中。如何将它们复制到隔离存储?我已经用一些代码回答了。我假设它们在隔离存储中?您不能从代码中编辑XAP中的任何文件。实际上,它们在XAP中。如何将它们复制到IsolatedStorage?我已经用一些代码回答了。虽然这将在WP7环境之外工作,但您不能在运行时执行此操作,因为WP7不支持这些方法。虽然这将在WP7环境之外工作,您不能在运行时执行此操作,因为WP7不支持这些方法。这给了我一个NullReferenceException。我将PNG_IMAGE替换为我的图像名(引号中),即“/Images/Person1.PNG”。引发的
NullReferenceException
在哪里?您是否将“image.png”从读取代码更改为“/Images/Person1.png”?请删除
/Images/Person1.png
中的第一个斜杠,使其仅为
Images/Person1.png
,并确保其构建类型为
Content
不,XAP中的所有文件都是只读的。您可以将映像复制到独立的存储中(这就是代码所做的)。你不能把它复制回XAP。对PNG所做的任何更改都必须保存在独立存储中,因此代码中需要新PNG文件的任何内容都应该指向IS而不是XAP。XAP将只保留原始的、未更改的PNG。哎呀,我的错。我已经编辑了代码。只需将
MemoryStream(data)
更改为
MemoryStream(streamData)
这会给我一个NullReferenceException。我将PNG_IMAGE替换为我的图像名(引号中),即“/Images/Person1.PNG”。引发的
NullReferenceException
在哪里?你换了电话号码了吗