如何制作一个持续运行的应用程序? [英] How to make a continuously running application?
问题描述
我有一个应用程序,该应用程序从给定目录中读取数据库文件列表,它将扫描这些文件列表及其大小,并且每当文件超过5 MB时,它将弹出警告消息.我现在的问题是如何使程序连续监视目录中的文件?因为目录中的数据库文件将不断增长,并且我不知道如何使应用程序连续监视文件并在文件达到特定大小时弹出警告消息?如果需要,这是我的代码:
I had make an application that read list of database files from a given directory and it will scan those list of files and it sizes and it will pop up a warning message whenever any of the files exceeding 5 MB. My problem now is that how to make the program continuously monitor the files in the directory? Because the database files in the directory will continuously growing and I don''t know how to make the application continuously monitor the files and pop up a warning message whenever a file had reach a certain size?? Here is my code if necessary :
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;
using System.Numeric;
namespace FileScan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.RowHeadersVisible = false;
DataGridViewImageColumn colIcon = new DataGridViewImageColumn();
colIcon.Name = "colIcon";
colIcon.HeaderText = "";
colIcon.ImageLayout = DataGridViewImageCellLayout.Zoom;
colIcon.DefaultCellStyle.SelectionBackColor = Color.White;
colIcon.Width = 18;
//colIcon.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(colIcon);
DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
colHold.Name = "colFileName";
colHold.HeaderText = "Name";
colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(colHold);
colHold = new DataGridViewTextBoxColumn();
colHold.Name = "colFileSize";
colHold.HeaderText = "Size";
colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(colHold);
colHold = new DataGridViewTextBoxColumn();
colHold.Name = "colFileType";
colHold.HeaderText = "Extension";
colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(colHold);
colHold = new DataGridViewTextBoxColumn();
colHold.Name = "colDateModified";
colHold.HeaderText = "Date Modified";
colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns.Add(colHold);
dataGridView1.RowTemplate.Height = 18;
//Replace the directory below with a directory on your computer.
string strDirLocal = @"C:\Documents and Settings\Administrator\Desktop\FILE";
int nRow = 0;
string sFileName = "";
decimal nFileSize = 0;
string sExt = "";
string sDateModified = "";
if (System.IO.Directory.Exists(strDirLocal))
{
foreach (string sPath in System.IO.Directory.GetFiles(strDirLocal, "*.*"))
{
Icon FileIcon = SystemIcons.WinLogo;
FileIcon = Icon.ExtractAssociatedIcon(sPath);
//Add the file to the TextBox, and remove the Path from the sPath string,
//leaving the file name.
dataGridView1.Rows.Add();
nRow = dataGridView1.Rows.Count - 1;
sFileName = sPath.Replace(strDirLocal + @"\", "");
sDateModified = System.IO.File.GetLastWriteTime(sPath).ToString();
//sFileSize = System.IO.File.ReadAllBytes(sPath).ToString();
nFileSize = System.IO.File.ReadAllBytes(sPath).Length;
nFileSize = nFileSize / 1024;
nFileSize = System.Math.Round(nFileSize);
if (nFileSize < 1)
{
nFileSize = 1;
}
if (System.IO.File.ReadAllBytes(sPath).Length == 0)
{
nFileSize = 0;
}
string[] sExtHold = sPath.Split('.');
sExt = sExtHold[sExtHold.Count() - 1];
dataGridView1.Rows[nRow].Cells[0].Value = FileIcon;
dataGridView1.Rows[nRow].Cells[1].Value = sFileName;
dataGridView1.Rows[nRow].Cells[2].Value = nFileSize;
dataGridView1.Rows[nRow].Cells[3].Value = sExt;
dataGridView1.Rows[nRow].Cells[4].Value = sDateModified;
//for (int rows = 0; rows < dataGridView1.RowCount; rows++)
//foreach (DataGridViewRow row in dataGridView1.Rows)
{ if (nFileSize > 5000)
{
MessageBox.Show("" + sFileName + " had reach its size limit.");
}
else
{}
}
}
}
}
}
}
推荐答案
您将需要Windows服务.并且您的监视功能必须从"Form_Load"移至计时器事件.
由于服务无法与屏幕交互(至少与更新的Windows版本不兼容),因此您将不得不更改通知方式:不是弹出消息框,而是向管理员发送电子邮件.
You will need a Windows Service. And your monitoring function must be moved from a "Form_Load" to a timer event.
Since a service cannot interact with the screen (at least with newer Windows versions), you''ll have to change the way of notification: instead of a message box popup, send an email to an administrator.
这篇关于如何制作一个持续运行的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!