C#创建文件夹和文件
2024-10-16 17:25:02
一、创建文件夹,例:
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
二、创建文件,例:
global::System.IO.FileInfo josnfile = new global::System.IO.FileInfo(JsonPath);
if (!josnfile.Exists)
{
// 创建map.json文件
FileStream fs = new FileStream(JsonPath, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write("[]");
sw.Flush();
sw.Close();
//Thread.Sleep(300);
}
三、遍历文件夹下的所有文件或文件夹
遍历文件:
//录像文件
string videoPath = fileManager.TrimEnd('\\') + "\\" + item.CourtID + "\\Conference\\" + item.ID;
if(Directory.Exists(videoPath))
{
DirectoryInfo TheFolder = new DirectoryInfo(videoPath);
//遍历文件
foreach (global::System.IO.FileInfo NextFile in TheFolder.GetFiles())
{
}
}
遍历文件夹:
if(Directory.Exists(videoPath))
{
DirectoryInfo TheFolder=new DirectoryInfo(videoPath);
//遍历文件夹
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
{
}
}
四、读取文件内容,例:
using (FileStream fs = new FileStream(JsonPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
{
noteIsSubmit = sr.ReadToEnd().ToString().Contains(FileName);
}
}
五、复制文件,例:
global::System.IO.FileInfo _f = new global::System.IO.FileInfo(path);
try
{
if (!_f.Exists)
{
//复制讲稿文件
global::System.IO.FileInfo copyFile = new global::System.IO.FileInfo(FileURL);
copyFile.CopyTo(path);
}
}
catch (Exception ex)
{
Logger.D("NoteMake讲稿制作发生异常:", ex.Message);
}
六、删除指定文件,例:
string path = FileManager.BASEPATH + "\\" + item.CourtID + "\\Topics\\" + item.ID + "\\" + item.Type + ".doc";
global::System.IO.FileInfo _f = new global::System.IO.FileInfo(path);
if (_f.Exists)
{
global::System.IO.File.Delete(path);
}
七、删除指定文件夹,例:
//讲稿标注文档路径
string noteFilePath = item.FilePath.Substring(, item.FilePath.LastIndexOf('.'));
if (Directory.Exists(noteFilePath))
{
DirectoryInfo _d = new DirectoryInfo(noteFilePath);
_d.Delete(true);//删除子目录和文件
}
最新文章
- jQuery的页面载入
- Distributed MVCC based cross-row transaction
- 全文搜索 Lucene.Net
- 在springmvc中配置jedis:
- 数迹学——Asp.Net MVC4入门指南(2):添加一个控制器
- System.exit(1)
- ToDoList:一款非常优秀的任务管理软件 —— 工具类
- Android配置----小米手机通过wifi连接ADB调试Android应用
- C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm
- STM32内存跟FLASH问题
- 将项目初始化到git服务器
- hdu 4143 A Simple Problem (变形)
- 真实代理(RealProxy)在WCF中的运用
- 58、js扩展
- linux dialog详解(图形化shell)
- 查看linux系统是运行在物理机还是虚拟机方法
- Pre-shared key
- Xcode 插件优缺点对照(推荐 20 款插件)
- 使用golang编写prometheus metrics exporter
- 测试教程网.unittest教程.3. 实例: 测试弱密码
热门文章
- Eureka服务注册过程详解之IpAddress(详解eureka.instance.prefer-ip-address = true 与 eureka.instance.prefer-ip-address)
- 最新的ES 5.0路由算法底层实现
- mysql日常运维
- hibernate多对一和一对多关联
- 了解SAGA
- 20165202 Mypwd
- Kali Linux更新源以及设置中文
- block ,GCD(转)
- tomcat部署和启动
- Flume-NG源码阅读之SpoolDirectorySource(原创)