File Path Directory总结
阅读目录
Path 对路径 字符串进行操作
- 获得后缀
- 能合并路径
- 获取文件名
Directory和DirectoryInfo 对目录进行操作
- 判断目录是否存在
- 创建目录
- 删除目录
- 获取目录下所有的子目录
- 获取目录下所有的子文件
File和FileInfo 对文件进行操作
- 读文件
- 写文件
- 追加文件
- 判断文件是否存在
- 创建文件
- 删除文件
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总结的更多相关文章
- 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)
C#获取文件名 扩展名 string fullPath = @"d:\test\default.avi"; string filename = Path.GetFileName(f ...
- C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)
文件流 FileStream 可读可写 大文件 释放 StreamReader 读取 释放 StreamWriter 写入 释放 using 中释放 File 可读可写 小文件 操作文 ...
- File、Directory、Path
File.Directory.Path https://blog.csdn.net/xiaouncle/article/details/52050577 File.Directory.Path是实际开 ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 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 ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- java.nio.file.Path
public interface Path extends Comparable<Path>, Iterable<Path>, Watchable 1. A Path repr ...
- 【转】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 ...
- Leetcode: Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
随机推荐
- Windows RDP远程连接CentOS 7
1. 打开已经安装了CentOS7的主机,以root用户登录,在桌面上打开一个终端,输入命令:rpm -qa|grep epel,查询是否已经安装epel库(epel是社区强烈打造的免费开源发行软 ...
- python3.6 实现AES加密的示例(pyCryptodome)
当然我也是通过官方推荐,使用下面命令去下载安装的,pip就是好用... pip install pycryptodome 撸码开始 废话不多说,直接上demo # from Crypto.Has ...
- CVE-2018-8420 漏洞复现
影响的 Windows 版本: Microsoft Windows 10 Version 1607 for 32-bit SystemsMicrosoft Windows 10 Version 160 ...
- python值解析excel
原文:http://blog.csdn.net/tomatoandbeef/article/details/52253578 一.安装python和xlrd模块 python下载地址,安装好后要配置环 ...
- Django---Xss过滤以及单例模式
Xss过滤 在表单填写的过程中我们就用到textarea,富文本编辑框,里面要用户输入相关的内容.如果有的人想要搞怪,在里面写一些js代码或者修改编辑的时候修改源代码,那提交上去之后就会使得页面显示不 ...
- node.js和npm离线安装
离线安装node.js和npm 1.下载官方安装包并拷贝到离线机器上. 官方下载地址:https://nodejs.org/en/download/ 2.解压文件: tar-xJf node-v8.9 ...
- DropDownList控件的使用方法
1. 使用代码添加数据 <asp:DropDownList ID="DropDownList1" runat="server"> </asp: ...
- Window Application has "update" key words
Error Qt Creater:console error:Failed to start program. Path or permissions wrong? Description 在使用Qt ...
- socket的protocal参数
Documentation for socket() on Linux is split between various manpages including ip(7) that specifies ...
- golang之切片与排序
1.排序与查找操作 排序操作在sort包中,sort.Ints对整数进行排序,sort.Strings对字符串进行排序,sort.Float64对浮点数进行排序 package main import ...