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

在工程下创建名为(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();
        }
    }
}


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();
        }
    }
}


使用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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的示例,演示如何使用 C# 操作 XML 文件: ```csharp using System; using System.Xml; public class XmlHandler { private XmlDocument xmlDoc; public XmlHandler(string xmlFilePath) { xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); } public XmlNodeList GetElementsByTagName(string tagName) { return xmlDoc.GetElementsByTagName(tagName); } public void AddChildNode(string parentNodeName, string childNodeName, string innerText) { XmlNode parentNode = xmlDoc.SelectSingleNode(parentNodeName); XmlElement childNode = xmlDoc.CreateElement(childNodeName); childNode.InnerText = innerText; parentNode.AppendChild(childNode); xmlDoc.Save(xmlDoc.BaseURI); } public void UpdateNodeValue(string nodeName, string newValue) { XmlNode node = xmlDoc.SelectSingleNode(nodeName); node.InnerText = newValue; xmlDoc.Save(xmlDoc.BaseURI); } public void DeleteNode(string nodeName) { XmlNode node = xmlDoc.SelectSingleNode(nodeName); node.ParentNode.RemoveChild(node); xmlDoc.Save(xmlDoc.BaseURI); } } ``` 在上面的代码示例中,我们创建了一个 `XmlHandler` 类,它可以加载 XML 文件,并提供了一些操作 XML 文件的方法: - `GetElementsByTagName(tagName)` 方法可以通过标签名获取节点列表。 - `AddChildNode(parentNodeName, childNodeName, innerText)` 方法可以在指定的父节点下添加一个子节点,并设置它的文本内容。 - `UpdateNodeValue(nodeName, newValue)` 方法可以更新指定节点的文本内容。 - `DeleteNode(nodeName)` 方法可以删除指定的节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈言必行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值