MoveTo
MoveTo(dest) перемещает файл: старый путь перестаёт существовать.
Синтаксис
fileInfo.MoveTo(destPath);
Пример
Давайте перенесём abcde.txt в 12345.txt:
using System;
using System.IO;
class Program
{
static void Main()
{
File.WriteAllText("abcde.txt", "abcde");
new FileInfo("abcde.txt").MoveTo("12345.txt");
Console.WriteLine(new FileInfo("abcde.txt").Exists);
Console.WriteLine(File.ReadAllText("12345.txt"));
}
}
Результат выполнения кода:
False
"abcde"
Пример
Давайте переместим файл в папку abcde:
using System;
using System.IO;
class Program
{
static void Main()
{
Directory.CreateDirectory("abcde");
File.WriteAllText("12345.txt", "12345");
new FileInfo("12345.txt").MoveTo("abcde/12345.txt");
Console.WriteLine(new FileInfo("abcde/12345.txt").Exists);
}
}
Результат выполнения кода:
True