I have an image, person1.png
, and four other images, person2.png
, person3.png
, person5.png
, and 开发者_如何学JAVAperson4.png
. I want to rename these images in C# code. How would I do this?
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
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
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);
}
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 :)
精彩评论