阅读目录

Path 对路径 字符串进行操作

  1. 获得后缀
  2. 能合并路径
  3. 获取文件名

Directory和DirectoryInfo  对目录进行操作

  1. 判断目录是否存在
  2. 创建目录
  3. 删除目录
  4. 获取目录下所有的子目录
  5. 获取目录下所有的子文件

File和FileInfo  对文件进行操作

  1. 读文件
  2. 写文件
  3. 追加文件
  4. 判断文件是否存在
  5. 创建文件
  6. 删除文件

1、Path类

 using System;
using System.IO;//目录和文件操作的名称空间
namespace _11_Path类 {
class Program {
static void Main(string[] args) {
string path = "c:\\abc\\1.txt" ;
//注意这里是对路径字符串的操作 而不是真正的文件 “修改”支持字符串层面的,没有真的给文件改名
path = Path.ChangeExtension(path, "avi" );//ChangeExtension()修改文件后缀名1.avi c:\\abc\\1.avi
//将两个路径合成一个路径,比用+好,可以方便解决是不是加斜线的问题,自动处理路径分隔符的问题
path = Path.Combine("c:\\abc\\def\\" , "1.jpg"); //c:\abc\def\1.jpg
//得到文件所在文件夹的位置 同样是从字符串的角度去处理
path = Path.GetDirectoryName(path);//c:\abc
path = Path.GetExtension(path);//扩展名 .txt
path = Path.GetFileName(path);//文件名.后缀名 1.txt
path = Path.GetFileNameWithoutExtension(path);//不含后缀的文件名 1
path = Path.GetFullPath("11-Path类.exe" );//文件全路径(相对文件的全路径 一般也不用此方法) F:\PIZIYIMAO\11-Path类\bin\Debug\11-Path类.exe
path = Path.GetTempFileName();//临时文件夹保存路径 自动创建文件 C:\Documents and Settings\PIZIYIMAO\Local Settings\Temp\tmp5E.tmp
path = Path.GetTempPath();//获取临时文件夹保存路径 C:\Documents and Settings\PIZIYIMAO\Local Settings\Temp\
Console.WriteLine(path);
Console.Read();
}
}
}

2、操作目录类 Directory与DirectoryInfo

 using System;
using System.IO;
namespace _12_Directory {
class Program {
static void Main( string[] args) {
DirectoryInfo dic = new DirectoryInfo( "c:\\abc" );
//dic.Name; //获取文件名
//dic.FullName; //获取文件全路径 功能要比Directory强大 区别在于它是实例类 而后者是静态类
Directory .CreateDirectory("c:\\abc" ); //创建文件夹
Directory .CreateDirectory("c:\\abc\\1\\2\\3\\4\\5\\6\\7" ); //连续创建多级文件夹
if (Directory .Exists( "c:\\abc")) //判断是否存在文件夹
{
Directory .Delete("c:\\abc" ); //如果存在则删除 如果文件夹为空能正常删除 不为空则会报错 "目录不是空的"
Directory .Delete("c:\\abc" , true); //true则指定 如果文件夹不为空 同样执行删除操作
}
string [] paths = Directory .GetDirectories( "c:\\abc"); //获取目录中所有子目录名称 注意只取下一级别 即c:\abc\1 如获取windows文件夹下所有文件夹路径可以使用此方法
string [] paths2 = Directory .GetDirectories( "c:\\windows", "$*" );//以上方法重载实现 检索以$开头的文件
string [] paths3 = Directory .GetDirectories( "c:\\abc", "*" , SearchOption .AllDirectories);//通配符查找文件夹中符合条件的文件 包括子级文件夹
foreach (string path in paths) {
Console .WriteLine(path);
}
string [] files = Directory .GetFiles( "c:\\windows"); //遍历文件夹下的所有文件
string [] files2 = Directory .GetFiles( "c:\\windows", "*.ini" , SearchOption .AllDirectories);//通配符查找目录下的文件 用法类似于GetDirectories
foreach (string file in files2) {
Console .WriteLine(file);
}
//目录操作最重要的就是 GetFiles和GetDirectories方法
Directory .GetParent("c:\\abc\\1\\2\\3\\4\\5\\6\\7" ); //返回7文件夹的父级目录 c:\abc\1\2\3\4\5\6
Console .Read();
}
}
}

3、文件类File

 using System;
using System.IO;
using System.Text;
namespace _13_File {
class Program {
static void Main( string[] args) {
//file静态类 使用file类时需要注意文件默认编码的使用 如果编码不正确 文件中会显示乱码
File .AppendAllText("c:\\1.txt" , "gb1232"); //向c:\\1.txt文件中追加 内容“gb2312”
//如果存在写入文件
if (File .Exists( "c:\\1.txt")) {
File .WriteAllText("c:\\1.txt" , "写入中文有时会出现乱码 需要使用第三个参数 指定Encoding文件的编码格式 Default为默认格式" ,Encoding .Default);//WriteAllText是彻底的覆盖 而AppendAllText是追加
}
//File.ReadAllText();//读取文件不再列举 以下方法查看文档不再举例
//string[] ReadAllLines(string path) //读取文本文件到字符串数组中
//string ReadAllText(string path) //读取文本文件到字符串中
//WriteAllLines(string path,string[] contents),//将字符串数组逐行保存到文件path中,会覆盖旧内容。
FileInfo fi = new FileInfo( "c:\\2.txt" );//实例化的类 功能比file要强大
fi.AppendText(); //它有很多的方法 和属性 自己查看 文档
Console .Read();
}
}
}

File Path Directory总结的更多相关文章

  1. 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)

    C#获取文件名 扩展名 string fullPath = @"d:\test\default.avi"; string filename = Path.GetFileName(f ...

  2. C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)

    文件流 FileStream  可读可写  大文件  释放 StreamReader 读取   释放 StreamWriter 写入   释放 using 中释放 File 可读可写  小文件 操作文 ...

  3. File、Directory、Path

    File.Directory.Path https://blog.csdn.net/xiaouncle/article/details/52050577 File.Directory.Path是实际开 ...

  4. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  5. RHEL 5.7 Yum配置本地源[Errno 2] No such file or directory

    在Red Hat Enterprise Linux Server release 5.7 上配置YUM本地源时,遇到了"Errno 5] OSError: [Errno 2] No such ...

  6. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  7. java.nio.file.Path

    public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path repr ...

  8. 【转】error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

    错误信息: /usr/local/memcacheq/bin/memcacheq: error while loading shared libraries: libevent-2.0.so.5: c ...

  9. Leetcode: Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

随机推荐

  1. application/json 和 application/x-www-form-urlencoded的区别

    public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...

  2. nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}

    Deploying inside Eclipse v3.6 raises the exception. The WEB-INF/classes/ folder in the .war doesn't ...

  3. Apache Hadoop 集群安装文档

    简介: Apache Hadoop 集群安装文档 软件:jdk-8u111-linux-x64.rpm.hadoop-2.8.0.tar.gz http://www.apache.org/dyn/cl ...

  4. 关于setConnectTimeout和setReadTimeout的问题

    1.问题描述 这几天测试重构后的下载框架,发现在下载过程中如果网络中断或网络较差,个别应用的下载就会阻塞卡住,一直卡在 “正在下载 xx%”.   2.问题排查和定位 思考:网络差不应该报网络异常的错 ...

  5. Prism之初识

    首先,简单地介绍说一下单一应用程序与复合应用程序. 一.单一应用程序 看看上面这张图片,假如我们当前的需求是实现主界面如图所示.如果将其构建成具有用户控件的传统 WPF 应用程序,首先应构建一个顶层窗 ...

  6. centos7虚拟机安装elasticsearch6.4.x-遇到的坑

    OS:Centos7x虚拟机 1H2Gjdk:1.8elasticsearch:5.6.0 1.下载“elasticsearch-5.6.0.tar.gz”解压到/usr/local/elastics ...

  7. 90. Subsets II (Back-Track, DP)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  8. maven项目 实现 spring mybatis 两个框架整合

    1.maven项目 src main java java源文件 resources 配置文件 beans.xml spring配置文件 <?xml version="1.0" ...

  9. js日期格式化 扩展Date()

    javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...

  10. DataStage 六、安装和部署集群环境

    DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 DataStage 三.配置ODBC Da ...