我正在MonoDroid(C#)制作应用程序,我正在尝试交换两个文件名,但它无效。
以下是我的代码
String x = file1.Name;
String y = file2.Name;
File f = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "Images/" + vehicleid + "/" + "temp" + ".jpg");
File tempfile1 = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "Images/" + vehicleid + "/" + x);
File tempfile2 = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "Images/" + vehicleid + "/" + y);
file1.RenameTo(file2);
file2.RenameTo(tempfile1);
答案 0 :(得分:1)
您是否尝试过System.IO.File.Move()?
如果旧文件和新文件位于同一物理磁盘上,则重命名该文件。否则,它会被物理移动(在新位置复制,然后 - 如果成功 - 删除旧文件)。