使用File、Path和Directory进行常见的操作
我们偶尔会用到文件操作,其中File、Path和Directory这三个类是比较常见的,今天写了一个测试demo,也是顺便学习一下,记录一二。
BTW,使用这几个类的时候需要引用using System.IO命名空间。
class Program
{
static void Main(string[] args)
{
//ProcessPath();
//ProcessFile();
//ReadFileByByteArray();
//ReadFileByAllLines();
//ReadFileByAllText();
//WriteFileByByteArray();
//WriteFileByLines();
//WriteFileByText();
UseDirectory();
Console.ReadKey();
}
/// <summary>
/// path类只是在string层面进行操作
/// </summary>
private static void ProcessPath()
{
string path = @"C:\Users\Administrator\Desktop\XX-Net.txt";
string directory = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string fileNamenoExtension = Path.GetFileNameWithoutExtension(path);
string fileExtension = Path.GetExtension(path);
string ss = Path.ChangeExtension(path, "csv");
Console.WriteLine($"directory is {directory},fileName is {fileName},file without extension is{fileNamenoExtension},extension is {fileExtension},new file is{ss} ");
}
private static void ProcessFile()
{
string path = @"C:\Users\Administrator\Desktop\2.txt";
//File.Create(path);
//File.Delete(path);
//File.Copy(path, @"C:\Users\Administrator\Desktop\3.txt");
File.Move(path, @"C:\Users\Administrator\Desktop\4.txt");
Console.WriteLine("creat successfully...");
}
private static void ReadFileByByteArray()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
byte[] fileByte = File.ReadAllBytes(path);
string fileByByte = Encoding.Default.GetString(fileByte);
Console.WriteLine(fileByByte);
}
private static void ReadFileByAllLines()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
string[] fileLines = File.ReadAllLines(path,Encoding.Default);
foreach (var line in fileLines)
{
Console.WriteLine(line);
}
}
private static void ReadFileByAllText()
{
string path = @"C:\Users\Administrator\Desktop\3.txt";
string fileText = File.ReadAllText(path,Encoding.Default);
Console.WriteLine(fileText);
}
private static void WriteFileByByteArray()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
string fileStr = "大家好,我来了,我的号码是1388888888";
byte[] fileByte = Encoding.Default.GetBytes(fileStr);
File.WriteAllBytes(path, fileByte);
Console.WriteLine("Write Sucessfully...");
}
private static void WriteFileByLines()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
string[] fileArray = { "你好","我的名字","叫Tom","我来自安徽省阜阳市太和县","他们","都知道我是个好人" };
File.WriteAllLines(path, fileArray);
Console.WriteLine("Write Sucessfully...");
}
private static void WriteFileByText()
{
string path = @"C:\Users\Administrator\Desktop\1.txt";
//File.WriteAllText(path, "落霞与孤鹜齐飞,秋水共长天一色。");
File.AppendAllText(path, "宜将剩勇追穷寇,不可沽名学霸王!");
Console.WriteLine("Write Sucessfully...");
}
private static void UseDirectory()
{
//Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\NewFolder");
//Directory.Delete(@"C:\Users\Administrator\Desktop\NewFolder",true);
string[] files = Directory.GetFiles(@"D:\迅雷下载", "*.rmvb",SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
Console.WriteLine(file);
}
Console.WriteLine("Get File Successfully...");
}
}
使用File、Path和Directory进行常见的操作的更多相关文章
- C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)
文件流 FileStream 可读可写 大文件 释放 StreamReader 读取 释放 StreamWriter 写入 释放 using 中释放 File 可读可写 小文件 操作文 ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode: Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- Leetcode算法比赛----Longest Absolute File Path
问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...
- Longest Absolute File Path -- LeetCode
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 【leetcode】388. Longest Absolute File Path
题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\ ...
随机推荐
- stl string常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- C#集合之并发集合
.NET 4 开始,在System.Collection.Concurrent中提供了几个线程安全的集合类.线程安全的集合可防止多个线程以相互冲突的方式访问集合. 为了对集合进行线程安全的访问,定义了 ...
- hosts文件原理
hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址.hosts文件通常用于补充或取代网络中DNS的功能.和DNS不同的是,计算机的用户可以直接对h ...
- 使用javascript生成当前博文地址的二维码图片
前面的话 在电脑端发现一篇好的博文,想在手机上访问.这时,就必须打开手机浏览器输入长长的URL地址才行,非常不方便.如果在博客标题的后面跟一张小的图片,点击该图片后,出现一张二维码的大图,然后再通过手 ...
- 1、在eclipse中导入Java的jar包方法---JDBC【图文说明】
1.Eclipse环境下jar包导入 在Eclipse环境下编写Java程序,常常会借用到各种jar包.如:连接数据库时,导入jar包是必须的.导入方法如下: 1.打开eclipse,右击要导入jar ...
- javascript闭包小结
对比看了几本书关于闭包的介绍,感觉<JavaScript面向对象编程指南>一书中介绍的最为清楚,其他部分书籍很多只讲结果,不讲具体原因,不易理解.总结如下 1.闭包的定义 如果一个函数会在 ...
- 你知道“移动端车牌识别”可以嵌入到PDA中应用吗?
一.移动端车牌识别产品描述 移动端车牌识别软件是基于移动平台的OCR识别应用程序,支持Android/IOS等多种主流移动操作系统.该产品只需通过智能手机或Pad的摄像头对准车牌,无需拍照,实现自动采 ...
- WPF 简易的跑马灯效果
最近项目上要用到跑马灯的效果,和网上不太相同的是,网上大部分都是连续的,而我们要求的是不连续的. 也就是是,界面上就展示4项(展示项数可变),如果有7项要展示的话,则不断的在4个空格里左跳,当然,衔接 ...
- markdown 基础语法
markdown 基础 你好,我是markdown文档 介绍 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 说的简单一点,mar ...
- Linux笔记③(ftp、nfs、ssh服务器搭建)
1.ftp服务器搭建(利用vsftpd这个工具) 作用:文件的上传和下载 服务器端: 修改配置文件,配置文件目录:/etc/vsftpd.conf ,修改里面的允许匿名访问.指定匿名访问目录等操作,根 ...