Rename File in IsolatedStorage

 ̄綄美尐妖づ 提交于 2019-12-19 02:05:08

问题


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


回答1:


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



回答2:


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.




回答3:


Perfectly execute this piece of code

string oldName="oldName";
string newName="newName";
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(oldName);
await file.RenameAsync(newName);


来源:https://stackoverflow.com/questions/736085/rename-file-in-isolatedstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!