【C#基础】Path类、File类、Directory类

本文详细介绍了C#中文件和目录操作的方法,包括使用Path类进行路径操作,File类进行文件的基本操作,如创建、复制、删除文件及读写内容,使用FileStream进行字节级的文件读写,以及如何复制多媒体文件。同时,文章还讲解了StreamReader和StreamWriter用于文本文件的读写,以及Directory类用于文件夹的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Path类(静态类,用于操作文件路径) 
Path类提供的常用静态方法: 
string str = @”C:\Users\YF105\Desktop\new.txt”; 
Path.GetFileName(str);//获取文件名,包含扩展名 
Path.GetFileNameWithoutExtension(str);//获取文件名但不包含文件扩展名 
Path.GetExtension(str);//获取文件扩展名 
Path.GetFullPath(str);//获取文件绝对路径 
Path.GetDirectoryName(str);//获取文件所在文件夹的名称 
Path.Combine(@”C:\Users\YF105\Desktop”, “Text.txt”);//将两个字符串组合成一个路径 
Path.ChangeExtension(str, “.mp3”);//更改文件扩展名

2.File类(操作文件) 
File.Create(@”C:\Users\YF105\Desktop\Test.txt”);//在指定路径创建文件 
File.Copy(@”C:\Users\YF105\Desktop\Test.txt”, @”C:\Users\YF105\Desktop\new.txt”);//复制一个文件 
File.Delete(@”C:\Users\YF105\Desktop\new.txt”);//删除一个文件

文本文件编码:将字符串以怎样的形式保存为二进制 
//UTF-8 
//GB2312:中文简体 
//GBK:包含简体和繁体 
//ASCII 
产生乱码的原因:保存文件所采用的编码格式和打开文件采用的编码格式不一样

a).File.ReadAllBytes和File.WriteAllBytes

byte[] buffer = File.ReadAllBytes(@”C:\Users\YF105\Desktop\test.txt”); 
string str = Encoding.UTF8.GetString(buffer);//将字节数组中的每一个元素按照指定的编码格式解码成字符串 
Console.WriteLine(str);

string str = “岁月别催,皆是皆非,遗憾终成空,遗憾不如梦”; 
byte[] buffer = Encoding.UTF8.GetBytes(str); 
File.WriteAllBytes(“test.txt”, buffer); 
Console.WriteLine(“OK”);

b) 
File.ReadAllLines(@”C:\Users\YF105\Desktop\Test.txt”);//读取每一行,返回一个字符数组 
File.ReadAllText(@”C:\Users\YF105\Desktop\Test.txt”);//读取所有内容,返回一个字符串

c)追加 
File.AppendAllText(@”C:\Users\YF105\Desktop\Test.txt”, “梦里她已去”);

3.文件流FileStream(操作字节)

将创建文件流对象的过程卸载using中,会自动帮我们释放文件流所占用的资源。

        static void Main(string[] args)
        {
            //FileStream读取数据
            using (FileStream fsRead = new FileStream(@"C:\Users\YF105\Desktop\TapToPlace.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 3];
                int r = fsRead.Read(buffer, 0, buffer.Length);
                string str = Encoding.UTF8.GetString(buffer, 0, r);
                Console.WriteLine(str);
            }

            //FileStream写数据
            using (FileStream fsWrite = new FileStream(@"C:\Users\YF105\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                string str = "遗憾终成空,遗憾不如梦,梦里她已去";
                byte[] buffer = Encoding.UTF8.GetBytes(str);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            Console.WriteLine("写入成功");            
        }

4.FileStream复制多媒体文件

    class Program
    {
        static void Main(string[] args)
        {
            string source = @"C: \Users\YF105\Desktop\虎二 - 光.mp3";
            string target = @"C: \Users\YF105\Desktop\泡泡 - 光.mp3";
            Copy(source, target);
            Console.ReadKey();           
        }

        public static void Copy(string source, string target)
        {
            byte[] buffer = new byte[1024 * 1024 * 5];
            int r;
            using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    while (true)
                    {
                        r = fsRead.Read(buffer, 0, buffer.Length);
                        if (r == 0)
                        {
                            break;
                        }
                        fsWrite.Write(buffer, 0, r);
                    }
                }

            }

        }
    }

5.StreamReader和StreamWriter(操作字符(文本文件))

6.Directory类(操作文件夹)

Directory.CreateDirectory(@”C:\Users\YF105\Desktop\Test”);//在指定路径创建文件夹 
Directory.Delete(@”C:\Users\YF105\Desktop\Test”,true);//删除指定文件夹 
Directory.Move(@”C:\Users\YF105”, @”C:\Users\YF105\Desktop\Test”);//将文件或目录及其内容移动到新位置 
Directory.GetFiles(@”C:\Users\YF105\Pictures”);//获得文件夹下所有文件的全路径 
Directory.GetFiles(@”C:\Users\YF105\Pictures”, “*.jpg”);//获得文件夹下所有的图片文件

7、绝对路径和相对路径
绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。
相对路径:文件相对于应用程序的路径。在开发中应该去尽量的使用相对路径。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值