Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename File in IsolatedStorage

I need to rename a file in the IsolatedStorage. How can I do that?

like image 969
Artur Carvalho Avatar asked Apr 09 '09 22:04

Artur Carvalho


2 Answers

There doesn't appear to anyway in native C# to do it (there might be in native Win32, but I don't know).

What you could do is open the existing file and copy it to a new file and delete the old one. It would be slow compared to a move, but it might be only way.

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());
}
like image 159
Samuel Avatar answered Oct 21 '22 02:10

Samuel


In addition to the copy to a new file, then delete the old file method, starting with Silverlight 4 and .NET Framework v4, IsolatedStorageFile exposes MoveFile and MoveDirectory methods.

like image 21
Matt Ellis Avatar answered Oct 21 '22 02:10

Matt Ellis