阅读目录

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. jenkins显示html样式问题的几种解决方案总结

    前言 jenkins上使用HTML Publisher plugin插件生成的html报告样式会丢失,需要设置下才能正常显示. 一.样式丢失 1.官方文档的解释如下,参考地址https://stack ...

  2. 什么是Emit,什么是反射,二者区别到底是什么?(转)

    Emit的准确定义,我们看看微软给出的答案 System.Reflection.Emit 命名空间包含{ 允许编译器或工具发出元数据和发出 Microsoft 中间语言 (MSIL) ,并可选择在磁盘 ...

  3. 对avalon的类名操作进行升级

    在对SVG元素进行类名操作时,发现有一个坑爹的事情,它的className竟然是一个对象,因此报一系列BUG.第一次想到的方法是添加setClasses, getClasses两个更底层的方法.于是相 ...

  4. hibernate查询出的实体,set值后,自动更新到数据库

    1.问题症状描述      最近在处理一个新需求问题,代码的大致逻辑是获取一个实体对象,调用该对象的set方法设置其中的某些字段,然后把修改后的实体作为参数供其他地方调用,根据返回值来决定是否更新这个 ...

  5. Openstack 组件简介

    1. Nova 计算服务: 负责承载和管理云计算系统 其中nova-compute service 通过调用Hypervisor APIs创建和终止虚拟机实例. 虚拟化技术: KVM和Xen 2. N ...

  6. CompletionPort

    [CompletionPort] 1.CreateIoCompletionPort.此方法用于创建与绑定.此方法调用后,socket即退出,只能通过CompletionKey来辨认是哪个socket的 ...

  7. python之daemon线程

    [python之daemon线程] A thread can be flagged as a “daemon thread”. The significance of this flag is tha ...

  8. Shared Libraries with Eclipse on 86_64 (64 bits) systems[z]

    If you followed my previous post http://linuxtortures.blogspot.com/2012/02/shared-libraries-with-ecl ...

  9. gnuc与ansic

    GNU c与标准c的区别 1) 零长度数组 struct var_data { int len; char data[0]; }test: int a; test.data -->a 2)cas ...

  10. 训练超参数, 出现 Cannot use GPU in CPU-only Caffe 错误?

    当我们用MNIST手写体数字数据库和LeNet CNN 模型训练超参数,运行 examples/mnist/train_lenet.sh是出现Cannot use GPU in CPU-only Ca ...