C# 文件、文件夹常规创建删除操作实例
原文链接:https://blog.csdn.net/weixin_45023644/article/details/121951840
C#的文件操作的功能是非常丰富的。他们大多来自System.IO类,比如:File、Directory、BinaryReader、BinaryWriter、DirectoryInfo、FileStream、MemoryStream、Path、StringWriter等等。
当然,其它很多类中也包含文件操作。
这里在用C# 举几个常见实例,来说明文件、文件夹的存在查询、创建和删除的几个基本方法,以及如何浏览打开。
string Path="D:\\a.txt";
if(File.Exists(Path))//确定指定的文件是否存在
{
//删除指定的文件
File.Delete( Path);
}
else
{
//在指定路径中创建或覆盖文件
File.Creat(Path);
}
二、Directory 类文件目录存在查询、删除和创建
Directory 类的功能是对目录和子目录进行创建、移动和枚举等。
文件目录的存在、删除和创建举例:
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种方法:
//获取模块的完整路径。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path2 = System.Environment.CurrentDirectory;
//获取应用程序的当前工作目录
string path3 = System.IO.Directory.GetCurrentDirectory();
//获取程序的基目录
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、打开资源管理器
//文件浏览器打开某个目录
//直接打开资源管理器(打开默认首页)
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浏览器
//直接打开IE浏览器(打开默认首页)
System.Diagnostics.Process.Start("Iexplore.exe");
//直接打开IE浏览器,打开指定页
System.Diagnostics.Process.Start("Iexplore.exe", "http://www.cnblogs.com/kissdodog");
浙公网安备 33010602011771号