C#编程-110:文件操作File静态类

简介: C#编程-110:文件操作File静态类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断文件是否存在
            //file是静态类
            string path = @"C:\Users\pengshiyu\Desktop\新建文本文档.txt";
            if (File.Exists(path)) Console.WriteLine("file \""+path+"\" is exists");
            else Console.WriteLine("file \""+path+"\" is not exists");
            //创建文件,注意:需要把创建的文件流关闭
            //方法一:try catch语句
            //方法二:先判断不存在,再创建
            string path1 = @"C:\Users\pengshiyu\Desktop\";
            if (!File.Exists(path1 + "newFile.txt"))
            {
                FileStream filestream = File.Create(path1 + "newFile.txt");
                filestream.Close();
                Console.WriteLine("文件创建成功!");
            }
            else
                Console.WriteLine("文件已经存在!");
            //打开文件
            //FileMode有六种枚举
            string path2 = @"C:\Users\pengshiyu\Desktop\test.txt";
            try
            {
                FileStream filestream = File.Open(path2,FileMode.Truncate);
                byte[] writebyte = { (byte)'p', (byte)'s', (byte)'y', (byte)',', (byte)'t', (byte)'e', (byte)'s', (byte)'t' };
                filestream.Write(writebyte,0,writebyte.Length);
                filestream.Close();
                Console.WriteLine("文件写入成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //文件复制
            string pathSource = @"C:\Users\pengshiyu\Desktop\source\test.txt";
            string pathDestination = @"C:\Users\pengshiyu\Desktop\destination\test.txt";
            if (File.Exists(pathSource))
            {
                try
                {
                    if (!File.Exists(pathDestination))
                    {
                        Console.WriteLine("请选择复制(1)还是移动(2):");
                        string choice = Console.ReadLine();
                        if (choice == "1")
                        {
                            //文件复制
                            File.Copy(pathSource, pathDestination, false);
                            Console.WriteLine("文件拷贝成功!");
                            Console.WriteLine("是否删除源文件?删除:1,不删除:2");
                            string delChoice = Console.ReadLine();
                            if (delChoice == "1")
                            {
                                //文件删除
                                File.Delete(pathSource);
                                Console.WriteLine("源文件删除成功!");
                            }
                            else if (delChoice == "2")
                            {
                                Console.WriteLine("不删除!");
                            }
                            else
                            {
                                Console.WriteLine("用户输入有误!");
                            }
                       }
                        else if (choice == "2")
                        {
                            //文件移动
                            File.Move(pathSource, pathDestination);
                            Console.WriteLine("文件移动成功!");
                        }
                        else
                        {
                            Console.WriteLine("文件存在,是否覆盖?是:1,否:2");
                            string choicecover = Console.ReadLine();
                            if (choicecover == "1")
                            {
                                File.Copy(pathSource, pathDestination, true);
                                Console.WriteLine("文件拷贝成功,覆盖完成!");
                            }
                            else if (choicecover == "2")
                            {
                                Console.WriteLine("文件拷贝失败,文件已存在!");
                            }
                            else
                            {
                                Console.WriteLine("输入有误!");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("源文件不存在");
            }
            Console.ReadKey();
        }
    }
}

110.1.jpg110.2.jpg110.3.jpg110.4.jpg110.5.jpg110.6.jpg110.7.jpg110.8.jpg

相关文章
|
4月前
|
Java
【文件操作】Java -操作File对象
【文件操作】Java -操作File对象
33 0
|
3月前
|
存储 C语言
文件操作及函数
文件操作及函数
39 0
|
8月前
|
存储 C语言 iOS开发
【C++】标准库 - 文件的读写 i/ofstream
本文章介绍 C++ 标准库中处理文件读写的 fstream ,以及其中的一些使用
106 0
C#编程-110:文件操作File静态类
C#编程-110:文件操作File静态类
C#编程-110:文件操作File静态类
|
C语言
【C 语言】文件操作 ( remove 函数删除文件 | rename 函数重命名文件 | 代码示例 )
【C 语言】文件操作 ( remove 函数删除文件 | rename 函数重命名文件 | 代码示例 )
392 0
【C 语言】文件操作 ( remove 函数删除文件 | rename 函数重命名文件 | 代码示例 )
|
C语言
【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )
【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )
180 0
【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )
|
C语言 数据安全/隐私保护
【C 语言】文件操作 ( getc 和 putc 函数 )(一)
【C 语言】文件操作 ( getc 和 putc 函数 )(一)
239 0
【C 语言】文件操作 ( getc 和 putc 函数 )(一)
|
数据安全/隐私保护 C语言
【C 语言】文件操作 ( getc 和 putc 函数 )(二)
【C 语言】文件操作 ( getc 和 putc 函数 )(二)
130 0
【C 语言】文件操作 ( getc 和 putc 函数 )(二)
|
程序员 C语言
c语言_文件操作_FILE结构体小解释
参考文档来自:https://www.cnblogs.com/haore147/p/3648395.html 我们通过fopen返回一个文件指针(指向FILE结构体的指针)来进行文件操作。 在vs2013下的代码如下: #include #pragma warning(disable:4996)...
1469 0