c#操作文件
操作文件夹
//文件路径 string fileFullPath = "D:\\HX\\File\\"; if (!Directory.Exists(fileFullPath)) { //路径未找到创建路径 Directory.CreateDirectory(fileFullPath); }
操作文件
//判断文件是否存在,不存在则创建的存在 string sPath = Environment.CurrentDirectory; sPath = Path.Combine("文件路径", "带后缀的文件名"); if (File.Exists(sPath)) { //创建该文件 不加Close 操作文件时报错提示占用 File.Create(sPath).Close(); //删除文件 File.Delete(fileFullName); }
文件相关属性
string gg = System.IO.Path.GetFullPath(opfd.FileName);//绝对路径 string wjlx = System.IO.Path.GetExtension(opfd.FileName);//文件扩展名 string wnm = System.IO.Path.GetFileNameWithoutExtension(opfd.FileName);//文件名没有扩展名 string kk = System.IO.Path.GetFileName(opfd.FileName);//得到文件 string lj = System.IO.Path.GetDirectoryName(opfd.FileName); //得到路径