C# 路径 目录 文件操作办法
1 using System; 2 using System.IO; 3 4 [, using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.IO; 9 10 namespace _07文件操作1_Path类 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 //Path类中方法,基本都是对字符串的操作,与实际文件都没关系。 17 //string path = @"C:\afds\aa\fdsf\fds\fsfs\fds\fdsfds\tesfdsf\a.txt"; 18 ////这里只是把字符串"C:\a\tes\a.txt"的后缀改成了"C:\a\tes\a.jpg",只是修改了字符串,与实际存储在磁盘上的文件 没有任何关系。 19 //path = Path.ChangeExtension(path, ".jpg"); 20 //Console.WriteLine(path); 21 22 //string s1 = @"c:\a\"; 23 //string s2 = "b.txt"; 24 //// string s3 = s1 + s2; 25 ////将两个路径连接起来,可以处理末尾的 \ 问题。 26 //string s3 = Path.Combine(s1, s2); 27 //Console.WriteLine(s3); 28 29 30 31 //string s1 = @"c:\windows\test\abc\a.txt"; 32 ////1.请获取路径中的文件名 33 //Console.WriteLine(Path.GetFileName(s1)); 34 ////2.请获取路径中的目录,不包含文件名 35 //Console.WriteLine(Path.GetDirectoryName(s1)); 36 37 ////3.获取文件名,不包含扩展名 38 //Console.WriteLine(Path.GetFileNameWithoutExtension(s1)); 39 40 ////4.只获取扩展名 41 //Console.WriteLine(Path.GetExtension(s1)); 42 43 44 45 46 ////获取完整路径================ 47 ////string txt = File.ReadAllText("a.txt"); 48 ////Console.WriteLine(txt); 49 ////Console.ReadKey();' 50 ////获取相对路径对应的绝对路径。 51 //Console.WriteLine(Path.GetFullPath("../../../../../../../../../../a.txt")); 52 53 54 //获取当前用户的临时目录。 55 // Console.WriteLine(Path.GetTempPath()); 56 57 //获取一个随机的文件名,并在临时目录下创建这个文件 58 // Console.WriteLine(Path.GetTempFileName()); 59 60 //获取一个随机的文件名(也可以用作文件夹名。) 61 Console.WriteLine(Path.GetRandomFileName()); 62 63 64 Console.ReadKey(); 65 } 66 } 67 }
件操作1_Path类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace _13File类的基本操作 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 ////1.File类来判断文件是否存在? 14 //if (File.Exists(@"c:\1.txt")) 15 //{ 16 // Console.WriteLine("存在!"); 17 18 //} 19 //else 20 //{ 21 // Console.WriteLine("8存在!"); 22 //} 23 //File.Delete(@"c:\1.txt");//如果文件不存在也不报异常! 24 //Console.WriteLine("删除成gong!"); 25 26 27 File.Copy(@"c:\1.flv", @"d:\3.flv", true); 28 29 Console.WriteLine("拷贝成功!"); 30 31 //File.Move(@"c:\1.flv", @"d:\1.flv"); 32 //Console.WriteLine("剪切成功!"); 33 Console.ReadKey(); 34 } 35 } 36 }
File类的基本操作
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace _15文件流的基本操作 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 #region 通过文件流读取磁盘上的文件 14 15 ////1。创建文件流 16 //FileStream fsRead = new FileStream("1.txt", FileMode.Open); 17 18 ////2.创建缓冲区 19 //byte[] byts = new byte[fsRead.Length]; 20 21 ////3.开始读取 22 //int r = fsRead.Read(byts, 0, byts.Length); 23 24 ////4.关闭文件流、释放资源 25 //fsRead.Close(); 26 //fsRead.Dispose(); 27 28 //string msg = System.Text.Encoding.UTF8.GetString(byts); 29 //Console.WriteLine(msg); 30 //Console.WriteLine("ok"); 31 //Console.ReadKey(); 32 33 #endregion 34 35 #region 通过文件流写入文件 36 ////1.创建文件流 37 //FileStream fs = new FileStream(@"c:\mywords.txt", FileMode.OpenOrCreate); 38 39 //string msg = "我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤.我的名字叫邓坤."; 40 ////2.创建byte[] 41 //byte[] byts = System.Text.Encoding.UTF8.GetBytes(msg); 42 43 ////3.将byts中的内容写入文件中。 44 //fs.Write(byts, 0, byts.Length); 45 46 //4.关闭文件流 47 //fs.Flush(); 48 //fs.Close(); 49 //fs.Dispose(); 50 //Console.WriteLine("写入成功!"); 51 //Console.ReadKey(); 52 #endregion 53 54 #region using 55 //using (FileStream fs=new FileStream()) 56 //{ 57 // fs.Read(); 58 59 //} 60 61 #endregion 62 } 63 } 64 }
文件流
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace _05StreamReader与StreamWriter 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 #region StreamWriter演示 14 15 ////1.创建了一个写文本文件的文件流。 16 //using (StreamWriter sw = new StreamWriter(@"c:\writer.txt", true, Encoding.GetEncoding("gb2312"))) 17 //{ 18 // for (int i = 0; i < 100; i++) 19 // { 20 // sw.WriteLine(i + "=" + i * 10); 21 // } 22 23 //} 24 //Console.WriteLine("ok"); 25 //Console.ReadKey(); 26 27 28 29 #endregion 30 31 #region StreamReader演示 32 using (StreamReader sr = new StreamReader(@"c:\writer.txt")) 33 { 34 35 //while (!sr.EndOfStream) //只要没有读取到文件流末尾,就继续读取 36 //{ 37 // Console.WriteLine(sr.ReadLine()); 38 //} 39 40 41 //2. 42 //string result = sr.ReadLine(); 43 //while (result != null) 44 //{ 45 // Console.WriteLine(result); 46 // result = sr.ReadLine(); 47 //} 48 string result; 49 while ((result = sr.ReadLine()) != null) 50 { 51 Console.WriteLine(result); 52 } 53 } 54 Console.WriteLine("ok"); 55 Console.ReadKey(); 56 #endregion 57 } 58 } 59 }
StreamReader与StreamWriter
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace _08Directory类 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 //Directory 15 //1.创建一些个目录 16 //// Directory.CreateDirectory(@"c:\test\1"); 17 18 //for (int i = 0; i < 1000; i++) 19 //{ 20 // Directory.CreateDirectory(@"c:\test\" + i); 21 //} 22 //Console.WriteLine("ok"); 23 24 ////2.获取当前目录下的所有的直接子目录 25 ////string[] dirs = Directory.GetDirectories(@"c:\"); 26 //string[] dirs = Directory.GetDirectories(@"c:\","*I*",SearchOption.TopDirectoryOnly); 27 //foreach (string item in dirs) 28 //{ 29 // Console.WriteLine(item); 30 //} 31 ////string[] files = Directory.GetFiles(@"c:\"); 32 //string[] files = Directory.GetFiles(@"c:\","*.txt",SearchOption.TopDirectoryOnly); 33 //Console.WriteLine("========================================="); 34 //foreach (string item in files) 35 //{ 36 // Console.WriteLine(item); 37 //} 38 39 40 ////3.判断某个目录下是否存在某个文件夹 41 //string path = @"c:\test1"; 42 //if (Directory.Exists(path)) 43 //{ 44 // Console.WriteLine("存在test1目录"); 45 46 //} 47 //else 48 //{ 49 // Console.WriteLine("不存在test1目录。"); 50 //} 51 52 ////删除c:\test1目录 53 ////如果目录不存在则删除的时候报异常,所以一般删除前都会加一个Directory.Exists()判断目录是否存在。 54 ////Directory.Delete(路径),这个重载,只能删除空目录,如果目录下不为空,则报异常。 55 56 ////Directory.Delete(path); 57 //Directory.Delete(path, true); //true表示,将当前目录以及里面的所有内容都删除。 58 //Console.WriteLine("删除成功!"); 59 60 //Directory.Move(@"c:\Test.exe", @"c:\a\Test.yzk"); 61 62 ////重命名 63 //Directory.Move(@"c:\a", @"c:\abc"); 64 //Console.WriteLine("移动成功!"); 65 66 67 //把一个文件夹当做一个对象来使用。 68 DirectoryInfo dirInfo = new DirectoryInfo(@"c:\abc"); 69 70 //通过dirInfo对象,就可以获取c:\abc下的所有的文件与文件夹 71 FileInfo[] finfos = dirInfo.GetFiles(); 72 DirectoryInfo[] dinfos = dirInfo.GetDirectories(); 73 Console.ReadKey(); 74 } 75 } 76 }
08Directory类
转载于:https://www.cnblogs.com/-qq593790351/p/3212582.html
C# 路径 目录 文件操作办法相关推荐
- python目录及文件操作_python路径及文件操作,10.22,10.27
csv 和ini文件处理 作者: 一个小菜鸡 csv常见格式 nums options arguments 逗号分割值 comma-Separated Values CSV是一个被行 ...
- php 语言文件操作,php中目录文件操作详谈
php中目录文件操作详谈 首先介绍的是一个从目录读取的函数,opendir(),readdir(),closedir(),使用的时候是先打开文件句柄,以下是小编整理的php中目录,文件操作详谈,欢迎来 ...
- C语言目录文件操作补充
本章节补充一些目录和文件操作的更多知识,因为这些知识涉及到时间操作,所以放在时间操作之后的章节中介绍. access库函数 access函数用于判断当前操作系统用户对文件或目录的存取权限. 包含头文件 ...
- 每天一个linux命令(目录文件操作):【转载】Linux文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...
- Windows与网络基础:Windows基本命令-目录文件操作
概括:了解Windows,先从基础命令开始 目标:掌握Windows的系统文件及目录操作的一些命令,掌握Windows的网络相关的命令. 原因:排查一些网络,包括还有对文件的一些操作,需要用到命令会比 ...
- ios 获取沙盒文件名_iOS_沙盒(sandbox)机制及获取沙盒路径和文件操作(NSFileManager)...
⚠️版权声明:本文为博主原创文章,转载必须标明原文出处. 一. 每个iOS应用SDK都被限制在"沙盒"中,"沙盒"相当于一个加了仅主人可见权限的文件夹,苹果对 ...
- P8-Windows与网络基础-Windows基本命令-目录文件操作(cd、dir、md、rd、move、copy、xcopy、del)
该系列为"蜗牛学苑-网络安全"笔记,跟随课程加入自己见解,同时也为项目中碰到一些问题做了解答 大纲 目标 授课 前言 DOS命令的约定 1.目录和文件的应用操作 1.1 cd命令 ...
- Linux C编程--目录文件操作
1.stat 获取文件属性 #include <sys/types.h> #include <sys/stat.h> #includ ...
- 爪哇国新游记之三十三----目录文件操作
1.判断路径是文件还是目录 File subDir=new File("c:\\mp3"); if(subDir.isDirectory()){// 是目录 }File mp3=n ...
最新文章
- 扩展基于注解的spring缓存,使缓存有效期的设置支持方法级别-redis篇
- linux共享磁盘给指定ip,Linux 磁盘挂载和mount共享
- 谷歌大脑开源「数据增强」新招数:ImageNet准确率达85%,大神Quoc Le出品
- Jzoj3625 旅行(travel)
- CPU方案简介UIS8190 - LTE CAT.1模块
- leetcode72 编辑距离
- 基于ASP的课外培训在线教育毕设免费下载
- .NET2.0 事务处理
- 毕业论文实验方法重复,如何降重?
- 优化计算机组策略,windows系统优化--使你的计算机飞起来
- 怎样理解python是解释型语言
- Win10 如何关闭烦人的任务栏广告,驱动人生广告,2345广告
- mysql循环方法总结
- 2的立方根用计算机怎样摁,立方根练习2doc用计算器求立方根.doc
- 用户使用手册编写方法
- 数字集成电路物理设计学习总结——布图规划和布局
- 题目 1878: 蓝桥杯2017年第八届真题-青蛙跳杯子
- 微信全球MBA创新大赛麻省理工学院的WeChat Care团队夺魁
- 旧文备份:FFTW介绍
- 飞利浦 f718 java 微信_飞利浦 F718:手机界面图
热门文章
- python文本热点问题挖掘_基于文本挖掘的网络热点舆情分析
- 获取网页中所有的小客车摇号信息
- 【无标题】西门子S7-200SMART四种密码解密软件
- Unity3d制作菜单栏
- 苹果地图错误将驾车者导向沙漠:偏离70公里
- 关于网卡eth0、eth1以及服务器为什么要把内网和外网卡区分开
- 转:彼得·德鲁克:如何发挥人的长处?
- 360安全卫士造成网站不能访问的解决办法
- 计算机任务管理器不能打开,电脑任务管理器打不开详细解决方法 | 专业网吧维护...
- json 跟着黑马打的代码 但还是undefined。求解,