两个类功能差不多,File是静态方法实现的,Fileinfo通过实例方法实现的;
文件操作例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace file_Operations { class Program { static void Main(string[] args) { FileStream fs = null; StreamWriter writer = null; //文件路径 string path = "D:\\test.txt"; //判断路径下文件是否存在 if (!File.Exists(path)) { fs = File.Create(path); Console.WriteLine("新建一个文件:{0}",path); } else { fs = File.Open(path,FileMode.Open); Console.WriteLine("文件已存在直接打开"); } //初始化对象 writer = new StreamWriter(fs); writer.Write("test文本"); Console.WriteLine("向文本中写入数据"); //关闭缓存 writer.Flush(); writer.Close(); fs.Close(); Console.WriteLine("关闭数据流"); } } }
时间: 2024-11-03 05:43:33