求C# winform文件读写统计功能
求C# winform文件读写统计功能,用C#实现,要求如下
using System;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace demo
{
public partial class Form1 : Form
{
string dataPath = @"c:\data.txt";//建议data.txt另外放一个盘下,c盘根目录可能会有权限问题无法读取
public Form1()
{
InitializeComponent();
}
List<SumItem>result = null;//存储总成绩
private void button1_Click(object sender, EventArgs e)
{
var text = File.ReadAllText(dataPath, Encoding.UTF8);//注意如果data.txt存储编码不是utf-8自己改下这里的Encodeing
textBox1.Text = text;
//数据处理
result = text.Split('\n').Select(i =>
{
var arr = i.Split(',');//看数据文件应该是中文状态下的逗号,如果不是注意改这里的分隔符
return new SumItem { Name = arr[0], Sum = arr.Skip(1).Select(v => int.Parse(v)).Sum() };
}).ToList();
textBox2.Text = string.Join("\r\n", result.Select(i => i.Name + " " + i.Sum));
}
private void button2_Click(object sender, EventArgs e)
{
//排序,降序
textBox2.Text = string.Join("\r\n", result.OrderByDescending(i=>i.Sum).Select(i => i.Name + " " + i.Sum));
}
}
class SumItem
{
public string Name { get; set; }
public int Sum { get; set; }
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!拖一个Timer控件和一个Lable控件到窗体上
Timer控件属性设置
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
具体代码
string weekstr = "";
private void control_Load(object sender, EventArgs e)
{
timer1.Start();
//把得到的星期转换成中文
switch (DateTime.Now.DayOfWeek.ToString())
{
case "Monday": weekstr = "星期一"; break;
case "Tuesday": weekstr = "星期二"; break;
case "Wednesday": weekstr = "星期三"; break;
case "Thursday": weekstr = "星期四"; break;
case "Friday": weekstr = "星期五"; break;
case "Saturday": weekstr = "星期六"; break;
case "Sunday": weekstr = "星期日"; break;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString() + " " + weekstr;
}
我无法直接提供现成的代码来实现文件读写操作的统计,因为每个应用需求不同,需要根据具体情况进行实现。以下是C#文件读写操作的基本方法和思路,供您参考:
使用File.ReadAllBytes和File.WriteAllBytes方法实现文件读取和写入。这两个方法可以保证在短时间内读取或写入整个文件。
统计文件读取和写入次数,可以使用一个计数器变量实现。在文件读写的地方让计数器自增即可。
实现文件读写的监视器,可以在程序中实现。首先,你需要定义一个监视器类,这个类需要继承FileSystemWatcher类。在监视器类中,你需要重写FileSystemWatcher类的ProcessEvent方法,用来处理文件事件。当程序运行时,只需要实例化监视器类对象,并将它绑定到文件路径上即可。
在winform中展示统计结果可以使用DataGridView控件。DataGridView控件可以绑定数据源,并在表格中展示数据。将计数器变量存储的数据绑定到DataGridView控件中即可展示统计结果。
下面是代码示例:
using System.IO;
using System.Windows.Forms;
namespace WinFormDemo
{
public partial class Form1 : Form
{
private int readCount = 0;
private int writeCount = 0;
private FileSystemWatcher watcher = new FileSystemWatcher(@"C:\"); // 监视器监听C盘根目录下的文件
public Form1()
{
InitializeComponent();
// 给监视器绑定事件处理方法
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.Deleted += Watcher_Deleted;
watcher.Renamed += Watcher_Renamed;
// 设置监视器属性
watcher.EnableRaisingEvents = true; // 开启事件处理
watcher.IncludeSubdirectories = true; // 监视子目录
watcher.Filter = "*.*"; // 监视所有文件类型
}
private void Form1_Load(object sender, EventArgs e)
{
// 绑定DataGridView控件的数据源
dataGridView1.DataSource = new[] {
new { Type = "Read", Count = readCount },
new { Type = "Write", Count = writeCount }
};
}
private void Watcher_Renamed(object sender, RenamedEventArgs e)
{
// 文件重命名事件处理
}
private void Watcher_Deleted(object sender, FileSystemEventArgs e)
{
// 文件删除事件处理
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
// 文件创建事件处理
if (e.ChangeType == WatcherChangeTypes.Created)
{
if (e.FullPath.EndsWith(".txt")) // 只统计txt文件读写
{
++writeCount;
dataGridView1.DataSource = new[] {
new { Type = "Read", Count = readCount },
new { Type = "Write", Count = writeCount }
}; // 更新DataGridView控件中的数据源
}
}
}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
// 文件修改事件处理
if (e.ChangeType == WatcherChangeTypes.Changed)
{
if (e.FullPath.EndsWith(".txt")) // 只统计txt文件读写
{
++readCount;
dataGridView1.DataSource = new[] {
new { Type = "Read", Count = readCount },
new { Type = "Write", Count = writeCount }
}; // 更新DataGridView控件中的数据源
}
}
}
}
}