C# 文件、文件夹常规创建删除操作实例
原文链接:https://blog.csdn.net/weixin_45023644/article/details/121951840
C#的文件操作的功能是非常丰富的。他们大多来自System.IO类,比如:File、Directory、BinaryReader、BinaryWriter、DirectoryInfo、FileStream、MemoryStream、Path、StringWriter等等。
当然,其它很多类中也包含文件操作。
这里在用C# 举几个常见实例,来说明文件、文件夹的存在查询、创建和删除的几个基本方法,以及如何浏览打开。
1 2 3 4 5 6 7 8 9 10 11 12 | string Path= "D:\\a.txt" ; if (File.Exists(Path)) //确定指定的文件是否存在 { //删除指定的文件 File.Delete( Path); } else { //在指定路径中创建或覆盖文件 File.Creat(Path); } |
二、Directory 类文件目录存在查询、删除和创建
Directory 类的功能是对目录和子目录进行创建、移动和枚举等。
文件目录的存在、删除和创建举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | string FolderPath = "D:\\DIR" ; if (Directory.Exists(FolderPath ) == false ) //文件夹是否存在 { //在指定路径中创建所有目录和子目录,除非它们已经存在 Directory.CreateDirectory(FolderPath); //不存在,创建目录 } else { //删除指定的目录,并删除该目录中的所有子目录和文件 //TRUE表示删除所有子目录和文件,缺省或false则表示这个是空目录,否则不会抛出异常 Directory.Delete( FolderPath, true ); } |
三、文件浏览
这里的文件浏览不是说打开那个路径获取路径名,那个是Dialog类,而是说像打开我的电脑一样用资源管理器的方式打开,这种应用在不少工具软件上也是经常见到的。C#同样可以轻松实现。
1、先说获当前取路径方法
获取路径的方法很多,经常用到的就是获取当前路径:
原文链接:https://blog.csdn.net/liudong8510/article/details/17264297/
C# 获取当前路径7种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //获取模块的完整路径。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; <strong> //获取和设置当前目录(该进程从中启动的目录)的完全限定目录 string path2 = System.Environment.CurrentDirectory; //获取应用程序的当前工作目录 string path3 = System.IO.Directory.GetCurrentDirectory();</strong> //获取程序的基目录 string path4 = System.AppDomain.CurrentDomain.BaseDirectory; //获取和设置包括该应用程序的目录的名称 string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取启动了应用程序的可执行文件的路径 string path6 = System.Windows.Forms.Application.StartupPath; //获取启动了应用程序的可执行文件的路径及文件名 string path7 = System.Windows.Forms.Application.ExecutablePath; StringBuilder str= new StringBuilder(); str.AppendLine( "System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1); str.AppendLine( "System.Environment.CurrentDirectory:" + path2); str.AppendLine( "System.IO.Directory.GetCurrentDirectory():" + path3); str.AppendLine( "System.AppDomain.CurrentDomain.BaseDirectory:" + path4); str.AppendLine( "System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5); str.AppendLine( "System.Windows.Forms.Application.StartupPath:" + path6); str.AppendLine( "System.Windows.Forms.Application.ExecutablePath:" + path7); string allPath = str.ToString(); /* 输出结果 * System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\ System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\ System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE */ |
2、浏览器打开文件夹
打开浏览路径的方法也很多,经常用到的就是获取当前路径:
原文链接:https://blog.csdn.net/weixin_45792450/article/details/104204530
C# 调用打开资源管理器和浏览器:
1、打开资源管理器
1 2 3 4 5 6 7 8 9 | //文件浏览器打开某个目录 //直接打开资源管理器(打开默认首页) System.Diagnostics.Process.Start( "Explorer.exe" ); //资源管理器打开某个目录 System.Diagnostics.Process.Start( "Explorer.exe" , "E:\\" ); //资源管理器打开某个文件 System.Diagnostics.Process.Start( "Explorer.exe" , "E:\\555.txt" ); //资源管理器浏览选中某个文件 System.Diagnostics.Process.Start( "Explorer.exe" , "/select," + @"E:\555.txt" ); |
2、打开IE浏览器
1 2 3 4 | //直接打开IE浏览器(打开默认首页) System.Diagnostics.Process.Start( "Iexplore.exe" ); //直接打开IE浏览器,打开指定页 System.Diagnostics.Process.Start( "Iexplore.exe" , "http://www.cnblogs.com/kissdodog" ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2023-04-07 IIS问题:无法识别的属性“targetFramework”。请注意属性名称区分大小写
2023-04-07 Vs2015引用项目时一直有黄色的三角形感叹号
2023-04-07 IIS 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法