C#读取文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tiantian
{
    public partial class frmread : Form
    {
        public frmread()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fileOper();
            MessageBox.Show("读取完成!!!");
           
        }
        public static void readFile(Stream stream,byte[] by)
        {
            int offset = 0;
            int strleng = by.Length; //strlength字节数
            while (strleng>0)
            {
                int read = stream.Read(by, offset, strleng);
                if(read<=0)
                {
                    throw new EndOfStreamException("文件读取到"+read.ToString()+"失败!");

                }
                strleng -= read;
                offset += read;
            }
        }
        public void fileOper()
        {
            string[] str= Directory.GetDirectories(@"D:/读取文件", "w*");
            MessageBox.Show("文本文件的个数是"+str.Length.ToString());
            string path = @"d:/读取文件/wyz.txt";
            //判断文件是否存在,不存在覆盖它
            if(!File.Exists(path))
            {
                using(StreamWriter sw=File.CreateText(path))
                {
                    sw.WriteLine("hello");
                }
            }
            StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312"));
            string s = "";
            while((s=sr.ReadLine())!=null)
            {
                   
               this.textBox1.Text += s;
                  
           }
               
           
        }
        private void CheckJGP(string strPath)
        {
            string bbb = Path.GetFileName(strPath);
            MessageBox.Show(bbb);
            string aaa=Path.GetFileNameWithoutExtension(strPath);
            MessageBox.Show(aaa);
            string frilpath=Path.GetExtension(strPath);
            MessageBox.Show(frilpath);
            FileInfo file = new FileInfo(strPath);
            if (file.Exists)
            {
               
                if (file.Extension.ToString() ==".txt"|| file.Extension.ToString() ==".doc")
                {
                    MessageBox.Show("ok");
                }
                MessageBox.Show("no");
            }
          
        }
        //判断文件是否是最新的
        public void newFile()
        {
            foreach (string vFile in Directory.GetFiles(@"d:/读取文件")) // 遍历目录 
            {
                //MessageBox.Show(vFile);
                string aaa = Path.GetFileNameWithoutExtension(vFile);//读取文件名
                MessageBox.Show(aaa);

                if (File.GetLastWriteTime(vFile) >= DateTime.Now.AddHours(-1))// 如果文件是在一个小时内修改的
                {
                   
                    File.Move(vFile, @"d:/" + Path.GetFileName(vFile)); // 用Move()移动(复制文件)
                    MessageBox.Show("文件已经被复制完!!!");
                }
               
            }
            MessageBox.Show("没有最新的文件");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            newFile();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CheckJGP(@"d:/读取文件/");
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值