C# 文件操作--代码演示

举报
陈言必行 发表于 2021/08/13 22:37:07 2021/08/13
【摘要】 在工程下创建名为(TextFile1.txt)的文件,但是我们编写如下图的代码检测时,却没有找到此文件,,, 因为程序是在Debug目录下执行的,所以我们创建的文件需要在Debug目录下,,,我们通常用的方法是:右键TextFile1.txt—> 属性 —> 将复制到输出目录的属性,更改为始终复制或者较新时复制,如下图: 这样就可以查看到我们目录下的文...

在工程下创建名为(TextFile1.txt)的文件,但是我们编写如下图的代码检测时,却没有找到此文件,,,
1
因为程序是在Debug目录下执行的,所以我们创建的文件需要在Debug目录下,,,我们通常用的方法是:右键TextFile1.txt—> 属性 —> 将复制到输出目录的属性,更改为始终复制或者较新时复制,如下图:
2

这样就可以查看到我们目录下的文件了,,,


FileInfo 和 DirectoryInfo 的一些属性,方法,,,

using System;
using System.IO;

namespace 查看文件_文件夹
{ class Program { static void Main(string[] args) { //根据(文件名)查找文件: 相对路径(就是找到程序所在的路径) //绝对路径,加上文件的路径名 FileInfo fileinfo = new FileInfo("TextFile1.txt"); //======================文件 常用属性====================== //查看文件是否存在 Console.WriteLine(fileinfo.Exists); //查看文件名(文件名.后缀) Console.WriteLine(fileinfo.Name); //目录 Console.WriteLine(fileinfo.Directory); //长度 Console.WriteLine(fileinfo.Length); //是否只读 Console.WriteLine(fileinfo.IsReadOnly); //==========================文件 方法==================== //删除的是输出路径下的文件 fileinfo.Delete(); //复制 fileinfo.CopyTo("fiet1.txt"); //新建文件,,, FileInfo filetemp = new FileInfo("temp.doc"); if (filetemp.Exists)  //如果不存在 { filetemp.Create();  //创建文件 } //移动文件(重命名) fileinfo.MoveTo("TextFile2.txt"); //=======================文件夹的操作====================== //相对路径下创建 DirectoryInfo dir = new DirectoryInfo("Test"); if (dir.Exists) { dir.Create(); } //当前程序的debug目录 DirectoryInfo dirinfo = new DirectoryInfo(@"D:\documents\visual studio 2015\Projects\Csharp高级篇\018_文件操作_查看\bin\Debug"); Console.WriteLine(dirinfo.Exists); //是否存在 Console.WriteLine(dirinfo.Name); //名字 Console.WriteLine(dirinfo.Parent); //父目录  Console.WriteLine(dirinfo.Root); //跟目录  Console.WriteLine(dirinfo.CreationTime);  //创建时间 //创建子目录 dirinfo.CreateSubdirectory("Czhenya"); Console.ReadKey(); } }
}


  
 
  • 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
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

StreamReader 读取文件,,,
StreamWriter 写入文件,,,

namespace 文件的读写
{ class Program { static void Main(string[] args) { //创建文本文件的读取流 StreamReader reader = new StreamReader("TestFile1.txt"); //1,ReadLine()去取一行 //while (true) //{ // //读取一行字符串 // string str = reader.ReadLine(); // Console.WriteLine(str); // if (str == null) // { // break; // } //} /2,读取文件到结束,,接受然后输出就行了 reader.ReadToEnd(); //3,读取单个字符  ,使用如方法1一样的循环即可 char c =(char) reader.Read(); //关闭流 reader.Close(); //创建写入流   如果文件存在将会被覆盖 StreamWriter writer = new StreamWriter("FileText1.txt"); while (true) { string mes = Console.ReadLine(); if (mes == "q")  //表示输入遇到q退出 { break; } writer.WriteLine(mes); } writer.Close(); Console.ReadKey(); } }
}


  
 
  • 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
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

使用FileStream 完成文件复制,,,

using System;
using System.IO;

namespace 读写文件应用
{ class Program { static void Main(string[] args) { //读出流 FileStream read = new FileStream("TextFile1.txt", FileMode.Open); //写入流 FileStream write = new FileStream("TextFile1_副本.txt", FileMode.Create); byte[] data = new byte[1024]; while(true){ int length = read.Read(data, 0, data.Length); if (length == 0) { Console.WriteLine(length); break; } else { write.Write(data, 0, length); } } read.Close(); write.Close(); Console.ReadKey(); } }
}


  
 
  • 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

文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。

原文链接:czhenya.blog.csdn.net/article/details/78241256

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。