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:/读取文件/");
}
}
}