C# 文件与目录的基本操作

  1. 文件的创建和写入
 public void BtnCreateFile_Click()
        {
            string path = Application.dataPath + @"\Test.txt";
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("This is a test file.");
            sw.WriteLine("This is second line.");
            sw.Close();
            fs.Close();
 
            // 也可以这样创建 StreamWriter
            // StreamWriter sw = File.CreateText(path);
        }

2.读取文件,文件内容

  /// <summary>
    /// 读取文件
    /// </summary>
    public void b移动文件tnReadFile_Click()
    {
        string path = Application.dataPath + "\\Test.txt";
        text.text = string.Empty;
        if (File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            // 也可以这样创建 StreamReader
            // File.OpenText(path);
            string str = string.Empty;
            while (true)
            {
                str = sr.ReadLine();
                if (!string.IsNullOrEmpty(str))
                {
                    text.text += str + "\r\n";
                }
                else
                {
                    sr.Close();
                    fs.Close();
                    break;
                }
            }
        }
        else
        {
             text.text = "指定的路径下不存在此文件!";
        }
    }

  1. 追加文件内容
 /// <summary>
    /// 追加文件内容
    /// </summary>
    public void BtnAppendFile_Click()
          {
              string path = Application.dataPath + "\\Test.txt";
              FileStream fs = new FileStream(path, FileMode.Append);
              StreamWriter sw = new StreamWriter(fs);
              // 也可以这样创建 StreamReader
              // StreamWriter sw = File.AppendText(path);
              sw.WriteLine("This is three line.");
              sw.Close();
              fs.Close();
          }

4.复制文件

/// <summary>
    /// 复制文件
    /// </summary>
    public void BtnCopyFile_Click()
      {
          string oldPath = Application.dataPath + "\\Test.txt";
          string newPath = Application.dataPath + "\\TestClone.txt";
          File.Copy(oldPath, newPath);
      }

5.删除文件

  /// <summary>
        /// 删除文件
        /// </summary>
        public void BtnDeleteFile_Click()
        {
            string path = Application.dataPath + "\\TestClone.txt";
            File.Delete(path);
        }

6.移动文件

  /// <summary>
    /// 移动文件
    /// </summary>
    public void BtnMoveFile_Click()
    {
        string oldPath = Application.dataPath + "\\Test.txt";
        // 移动文件的同时也可以使用新的文件名
        string newPath = "C:\\NewTest.txt";
        File.Move(oldPath, newPath);
    }

7.创建目录

 /// <summary>
       /// 创建目录
       /// </summary>
       public void BtnCreateDirectory_Click()
       {
           string path1 = "C:\\Jason1";
           // 创建目录 Jason1
           DirectoryInfo dDepth1 = Directory.CreateDirectory(path1);
           // dDepth2 指向 dDepth1 创建的子目录 Jason2
           DirectoryInfo dDepth2 = dDepth1.CreateSubdirectory("Jason2");
           // 设置应用程序当前的工作目录为 dDepth2 指向的目录
           Directory.SetCurrentDirectory(dDepth2.FullName);
           // 在当前目录创建目录 Jason3
           Directory.CreateDirectory("Jason3");
       }

来源:https://blog.csdn.net/gaojinjingg/article/details/53116186

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值