[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace iGMASStest
{
public partial class WatchState : Form
{
int count = 0;
bool timerflag = true;
private bool bOrg = true;
public WatchState()
{
InitializeComponent();
}
private void WatchState_Load(object sender, EventArgs e)
{
//整体更改单元格字体属性
Font a = new Font("GB2312", 15);
dataGridView1.Font = a;
//定义表头
dataGridView1.ColumnHeadersVisible = true;
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
columnHeaderStyle.BackColor = Color.Beige;
columnHeaderStyle.Font = new Font("Verdana", 18, FontStyle.Bold);
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
//表格自动填充
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
//定义列
dataGridView1.ColumnCount = 5;
dataGridView1.Columns[0].Name = "监控业务";
dataGridView1.Columns[1].Name = "预计执行时间";
dataGridView1.Columns[2].Name = "完成情况";
dataGridView1.Columns[3].Name = "业务运行状态报告";
dataGridView1.Columns[4].Name = "对应日志文件";
string strProgramExePath = Application.StartupPath;
string[] DoneOrNot = new string[6];
string[] state = new string[6];
string[] FileName = new string[6];
string[] strLogFilePath = new string[6];
string[] TimeGroup = new string[6] { "2014-08-11 08:00:00", "2014-08-11 09:00:00", "2014-08-11 10:00:00", "2014-08-11 11:00:00", "2014-08-11 12:00:00", "2014-08-11 15:09:00" };
string[] FindFile = new string[6];
for (int i = 0; i < 6;i++ )
{
DateTime ModifTime = DateTime.Parse(TimeGroup[i]);
FindFile[i] = ModifTime.ToString("yyyyMddHH");
}
//各文件所在的路径
string startlogfile = Application.StartupPath+"\\Test";
string DirName = startlogfile;
DirectoryInfo dir = new DirectoryInfo(DirName);
//如果非根路径且是系统文件夹则跳过
if (null != dir.Parent && dir.Attributes.ToString().IndexOf("System") > -1)
{
return;
}
//取得所有文件
int[] flag2 = new int[6];//指定路径是否含有所需的文件标志,0“无”1“有”
int num = 0;
FileInfo[] finfo = dir.GetFiles();
for (int i = 0; i < 6; i++)
{
strLogFilePath[i] = startlogfile + "\\bac_drm_" + FindFile[i] + ".log";
FileName[i] = "bac_drm_" + FindFile[i] + ".log";
}
string fname = string.Empty;
for (int j = 0; j < 6;j++ )
{
for (int i = 0; i < finfo.Length; i++)
{
fname = finfo[i].Name;
//判断文件是否包含查询名
if (fname.IndexOf(FileName[j]) > -1)
{
flag2[j] = 1;
// Console.WriteLine(finfo[i].FullName);
num++;
}
}
}
DateTime[] DT = new DateTime[6];
for (int i = 0; i < 6;i++ )
{
DT[i] = Convert.ToDateTime(TimeGroup[i]);
System.DateTime CT = DateTime.Now;
if (CT<DT[i])
{
DoneOrNot[i] = "未完成";
state[i] = "未运行";
}
else
{
if (flag2[i] == 1)
{
StreamReader sr = new StreamReader(strLogFilePath[i], Encoding.Default);
string strContent = sr.ReadToEnd();
sr.Close();//关闭文件
bool b = strContent.Contains("error");//判断是否有error
if (b)
{
state[i] = "运行故障报警";
DoneOrNot[i] = "已完成";
}
else
{
state[i] = "正常运行结束";
DoneOrNot[i] = "已完成";
}
}
else
{
DoneOrNot[i] = "等待执行";
state[i] = "等待运行";
}
}
}
//定义行
string[] row1 = new string[] { "数据质量评估", TimeGroup[0], DoneOrNot[0], state[0], FileName[0] };
string[] row2 = new string[] { "快速产品质量评估", TimeGroup[1], DoneOrNot[1], state[1], FileName[1] };
string[] row3 = new string[] { "超快速产品质量评估", TimeGroup[2], DoneOrNot[2], state[2], FileName[2] };
string[] row4 = new string[] { "最终产品质量评估", TimeGroup[3], DoneOrNot[3], state[3], FileName[3] };
string[] row5 = new string[] { "xx产品质量评估", TimeGroup[4], DoneOrNot[4], state[4], FileName[4] };
string[] row6 = new string[] { "完好性产品质量评估", TimeGroup[5], DoneOrNot[5], state[5], FileName[5] };
object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };
dataGridView1.AllowUserToAddRows = false;
// TEST2.dataGridView1
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
int ss = dataGridView1.Rows.Count;
//定义行高
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Height = 75;
}
dataGridView1.AutoResizeColumnHeadersHeight();//定义表头高
//隔行变色
if (this.dataGridView1.Rows.Count != 0)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; )
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightCyan;
i += 2;
}
}
// this.dataGridView1[3, 3].Style.ForeColor = Color.Green;//字体变色
// dataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Red;//行变色
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
timer1.Enabled = true;
//业务监控颜色显示
for (int j = 2; j < 4; j++)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string s = "";
s = dataGridView1.Rows[i].Cells[j].Value.ToString();
switch (s)
{
case "等待执行": dataGridView1[j, i].Style.BackColor = Color.Yellow; break;
case "等待运行": dataGridView1[j, i].Style.BackColor = Color.Yellow; break;
case "已完成": dataGridView1[j, i].Style.BackColor = Color.LightGreen; break;
case "正常运行结束": dataGridView1[j, i].Style.BackColor = Color.LightGreen; break;
case "运行故障报警": dataGridView1[j, i].Style.BackColor = Color.OrangeRed; break;
case "未完成": dataGridView1[j, i].Style.BackColor = Color.Gray; break;
case "未运行": dataGridView1[j, i].Style.BackColor = Color.Gray; break;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (bOrg)
{
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
{
if (this.dataGridView1[i, j].FormattedValue.ToString().IndexOf("运行故障报警") >= 0)
this.dataGridView1[i, j].Style.BackColor = Color.White;
}
}
}
else
{
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
{
if (this.dataGridView1[i, j].FormattedValue.ToString().IndexOf("运行故障报警") >= 0)
this.dataGridView1[i, j].Style.BackColor = Color.Red;
}
}
}
bOrg = !bOrg;
count++;
if (count == 3600)
{
timerflag = false;
}
if (timerflag)
{
timer1.Enabled = true;
//timer1.Start();
timer1.Interval = 500;//0.5s
}
else
{
timer1.Enabled = false;
}
}
}
}