movefile.cs

来自「csharp课本的源代码」· CS 代码 · 共 37 行

CS
37
字号
using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
        try
        {
            if (!File.Exists(path))
            {
                using (FileStream fs = File.Create(path)) { }
            }
            //确定目标不存在
            if (File.Exists(path2))
                File.Delete(path2);
            //移动文件
            File.Move(path, path2);
            Console.WriteLine("文件由{0}移动到{1}", path, path2);
            //判断文件是否存在
            if (File.Exists(path))
            {
                Console.WriteLine("源文件存在");
            }
            else
            {
                Console.WriteLine("源文件不存在");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("操作取消:{0}", e.ToString());
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?