Rename File in IsolatedStorage
Asked Answered
C

3

8

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

Cloverleaf answered 9/4, 2009 at 22:15 Comment(0)
H
9

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());
}
Hughes answered 9/4, 2009 at 22:36 Comment(2)
I suppose it is only slow if the file is really big. Maybe a couple of MB should be irrelevant. Going to try it. Thanks SamuelCloverleaf
Works great! No noticeable overhead.Cloverleaf
S
7

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.

Seal answered 12/2, 2010 at 2:56 Comment(0)
A
1

Perfectly execute this piece of code

string oldName="oldName";
string newName="newName";
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(oldName);
await file.RenameAsync(newName);
Aquiver answered 15/9, 2014 at 11:25 Comment(1)
This answer uses the new WinRT APIs, and not IsolatedStorage, as requested by the OPGagger

© 2022 - 2024 — McMap. All rights reserved.