File、Directory、Path

https://blog.csdn.net/xiaouncle/article/details/52050577

File、Directory、Path是实际开发中应用频率比较高的类,程序对电脑的简单操作基本可以概括为对文件、目录、路径的操作,下面讲解主要用法:

File的用法
string filePath=@"d:\Instrument\guitar.txt";
string destinationFilePath=@"d:\Favorite\guitar.txt";
string moveFilePath=@"d:\Garbage\guitar.txt";
if (File.Exists(filePath))
{
Console.WriteLine("存在文件" + filePath);
}
else
{
//Create时,文件夹d:\Instrument必须存在,否则会报错
File.Create(filePath);
}
//Copy时,文件夹d:\Favorite必须存在,否则会报错
File.Copy(filePath, destinationFilePath, true);
//Move时,文件夹d:\Garbage必须存在,否则会报错
//Move时,如果moveFilePath值为d:\Garbage,
//那会创建一个名为Garbage的文件(没有后缀名)
File.Move(filePath, moveFilePath);
//Delete时没有目标文件不会报错
File.Delete(moveFilePath);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Directory的用法
string directory=@"d:\Food\Meat";
string sourceDirName=@"d:\Music\Beyond";
string destDirName=@"d:\07\FavoriteMusic";
if (Directory.Exists(directory))
{
Console.WriteLine("目录" + directory + "存在");
}
else
{
//无论目录D:\Food是否存在,D:\Food\Meat都能成功创建
Directory.CreateDirectory(directory);
}
//Move指的是把D:\Music中的Beyond文件夹,移动到D:\07文件夹中,
//然后重命名为FavoriteMusic
//D:\07文件夹必须存在否则会报错
//如果D:\07中已经存在名为FavoriteMusic的文件夹那程序会报错
//Beyond文件夹中的文件或子目录在移动过后还会保持原样
//Move不能跨盘符移动
Directory.Move(sourceDirName, destDirName);

//只能删除空目录,包含文件或子目录都无法删除
Directory.Delete(@"d:\test");
//删除目录,及其文件和子目录
Directory.Delete(@"d:\test",true);

//搜索"e:\学习"目录下的所有txt文件,不搜索子目录
string[] files = Directory.GetFiles(@"e:\学习", "*.txt", SearchOption.TopDirectoryOnly);
//搜索"e:\学习"目录下的所有文件,搜索子目录
string[] allFiles = Directory.GetFiles(@"e:\学习", "*", SearchOption.AllDirectories);
//后边的两个参数可以不填,默认搜索顶层目录的所有文件
string[] normalFiles = Directory.GetFiles(@"e:\学习");

//搜索"e:\学习"目录下的所有名字中包含“课”的文件夹,不搜索子目录
string[] directories = Directory.GetDirectories(@"e:\学习", "*课*", SearchOption.TopDirectoryOnly);
//搜索"e:\学习"目录下的所有名字中包含“课”的文件夹,搜索子目录
string[] allDirectories = Directory.GetDirectories(@"e:\学习", "*", SearchOption.AllDirectories);
//后边的两个参数可以不填,默认搜索顶层目录的所有文件夹
string[] normalDirectories = Directory.GetDirectories(@"e:\学习");
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
35
36
37
38
Path的用法
string path = @"d:\Entertainment\Music\在雨中.mp3";
//fullPath==@"d:\Name\guoguo"
string fullPath = Path.Combine(@"d:\Name", "guoguo");
//root==@"d:\"
string root = Path.GetPathRoot(path);
//@"d:\Entertainment\Music"
string directoryName = Path.GetDirectoryName(path);
//fileName="在雨中.mp3"
string fileName = Path.GetFileName(path);
//fileNameWithoutExtension="在雨中"
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
//extension=".mp3"
string extension = Path.GetExtension(path);
//newPath=@"d:\Entertainment\Music\在雨中.avi"
//path的值不会改变
string newPath = Path.ChangeExtension(path, "avi");
//GetFullPath获取的是当前可执行程序的路径
//myFullPath=@"E:\Projects\20160725Share\20160725Share\bin\Debug\Data\text.txt"
string myFullPath = Path.GetFullPath(@"Data\text.txt");
---------------------
作者:changuncle
来源:CSDN
原文:https://blog.csdn.net/xiaouncle/article/details/52050577
版权声明:本文为博主原创文章,转载请附上博文链接!

File、Directory、Path的更多相关文章

  1. C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)

    (FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding)     C#文 ...

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

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

  3. C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

        1.管理文件系统 一般而言,应用程序都会有保存数据.检索数据的需求. 1.1 使用 path 类来访问文件路径 [path常用的方法]:http://www.cnblogs.com/tangg ...

  4. C#对文件/目录的操作:Path、File、Directory、FileStream、StreamReader、StreamWriter等类的浅析

    以下类的命名空间都是:System.I/0; 一.Path:主要对文件路径的操作! 常用方法: String path=@"C:\a\b\c\123.txt"; 1-1.Path. ...

  5. C#中使用Path、Directory、Split、Substring实现对文件路径和文件名的常用操作实例

    场景 现在有一个文件路径 E:\\BTSData\\2019-11\\admin_20180918_1_1_2 需要获取最后的文件名admin_20180918_1_1_2 需要获取文件的上层目录20 ...

  6. System.IO中的File、FileInfo、Directory与DirectoryInfo类(实例讲解)

    一.建立的文件夹(对这些文件进行以上四个类的操作): 父目录: 父目录的子目录以及父目录下的文件: 子目录下的文件: 二.效果图 三.代码实现 using System; using System.I ...

  7. File类、FileInfo类、Directory类、DirectoryInfo类

    File类.Directory类,都是静态类,可以直接使用类名 FileInfo类.DirectoryInfo类,都是动态类,需要new对象,通过对象来操作 [文件的创建.复制.移动.删除]using ...

  8. Non Lasting Storage File System、procfs、sysfs

    catalog . 引言 . proc文件系统 . 简单的文件系统 . sysfs 0. 引言 传统上,文件系统用于在块设备上持久存储数据,但也可以使用文件系统来组织.提供.交换并不存储在块设备上的信 ...

  9. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

随机推荐

  1. NGUI实现简单的倒计时组件

    using System; using UnityEngine; public enum ETimerType { CommonFormat, // 78 77 76 75 ... TimeForma ...

  2. Android开发中需要注意哪些坑

    作为一个有两.三年Android应用开发经验的码农,自然会遇到很多坑,下面是我能够想起的一些坑(实践证明不记笔记可不是个好习惯),后面有想到其它坑会陆续补上. 1.在Android library中不 ...

  3. luogu P2408 不同子串个数

    考虑反向操作,去计算有多少组相同的子串,对于一组大小为k的极大相同子串的集合,ans-=k-1. 为了避免重复计算,需要一种有效的,有顺序的记录方案. 比如说,对于每一个相同组,按其起始点所在的位置排 ...

  4. 浅谈Linux

    Linux系统最初由芬兰赫尔辛基大学的Andrew S.Tanenbaum写的MINIX操作系统演变而来,这是一个小型操作系统,主要用于教学,1991年1月,Tanenbaum的学生Linus Tor ...

  5. python-day42--单表查询

    1. 简单查询select * from employee;select name,salary from employee; 2. where条件           1.比较运算符:> &l ...

  6. Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown

    原因是:没有引入tomcat依赖包 <dependency> <groupId>org.springframework.boot</groupId> <art ...

  7. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...

  8. PHP中输出本地时间

    <?php header("Content-Type:text/html;charset=utf-8"); // 输出日 echo date("l") . ...

  9. bzoj1081

    题解: 先暴力找规律 然后就一加一减的枚举 代码: #include<bits/stdc++.h> using namespace std; ],p[]; int main() { sca ...

  10. Mysql 中Left/Right join on后面and和where条件查询的差异-Mysql SQL运算符是有优先级

    一.Mysql中Left/Right join on后面and和where条件查询的差异 1.建两张测试表,一张商户定义表.一张商户操作状态明细表 1)商户定义表 CREATE TABLE hope. ...