using System; using System.Collections.Generic; using System.Text; using System.IO; namespace V18.__文件命名空间及基础概念 { class Program { static void Main(string[] args) { //判断文件是否存在 string path = @"D:\桌面文件\新建 文本文档.txt"; if (File.Exists(path)) { Console.WriteLine("存在文件:" + path); } else { Console.WriteLine("不存在文件:" + path); //创建文件 try { FileStream newtext = File.Create(path); newtext.Close(); Console.WriteLine("文件创建成功"); } catch (Exception ex) { Console.WriteLine("文件创建失败!原因是:" + ex.ToString()); } } //打开文件 //Open(string path,FileMmode mode) //FileMode的六种枚举 //1.Append 打开现有文件并定位至文件结尾,或创建新文件 //2.Create 创建新文件 如存在 它将被改写 //3.CreateNew 创建新文件 如存在 将引发异常 //4.Open 打开现有文件 //5.OpenOrCreate 如文件存在 打开;如不存在,创建新文件 //6.Truncate 打开现有文件,文件一旦被打开 将被截断为0字节大小 //FileStream newca = File.Open(path, Append); Console.ReadKey(); } } } |
|
来自: 时间剧毒 > 《第二十章:文件系统相关技术》