c#窗体应用程序实现只要开启软件,就启动监视器实时监控文件夹中新生成的TXT文件,并从TXT文件名称中获取条码(TXT文件名称前27位,包括数字,字母,_,-),从建立到结束的时间,烧录结果等信息。如果文件名后缀是PASSED.txt,OK数就+1,如果是FAILED.txt,NG数就+1,然后计算合格率,测试总数。并在窗体建立OK数,NG数,测试总数,合格率的textbox组件,将上面的结果在组件中显示出来。并在窗体上创建DataGridview组件,显示出序号(如果是第一个文件夹,序号就是1,第二个就是2,以此类推),条码,时间,烧录结果的信息。如果判定为pass,在DataGridview那一行为绿色,判定为fail,显示为红色。最后将生成的TXT文件转换为csv格式,并在csv头部添加测试的信息,然后将编辑好的csv文件保存到另一个文件夹中。另外新生成的文件同样执行以上操作,相同名字的文件结果直接覆盖,不会受线程的影响。测试数据保存,下次打开软件,数据不会清零
时间: 2023-06-15 15:06:42 浏览: 153
这是一个比较复杂的需求,需要使用多线程和文件IO操作。以下是一个示例代码,仅供参考:
```
public partial class Form1 : Form
{
private FileSystemWatcher watcher;
private int okCount;
private int ngCount;
private int totalCount;
private double passRate;
private int folderIndex;
private List<TestResult> testResults;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 初始化计数器和测试结果列表
okCount = Properties.Settings.Default.OKCount;
ngCount = Properties.Settings.Default.NGCount;
totalCount = okCount + ngCount;
passRate = (double)okCount / totalCount;
folderIndex = Properties.Settings.Default.FolderIndex;
testResults = new List<TestResult>();
// 初始化文本框显示
textBoxOKCount.Text = okCount.ToString();
textBoxNGCount.Text = ngCount.ToString();
textBoxTotalCount.Text = totalCount.ToString();
textBoxPassRate.Text = passRate.ToString("P");
// 初始化DataGridView
dataGridView.AutoGenerateColumns = false;
dataGridView.DataSource = testResults;
// 初始化文件监视器
watcher = new FileSystemWatcher();
watcher.Path = Properties.Settings.Default.FolderPath;
watcher.Filter = "*.txt";
watcher.Created += Watcher_Created;
watcher.EnableRaisingEvents = true;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
// 获取文件名和目录
string fileName = Path.GetFileNameWithoutExtension(e.FullPath);
string directory = Path.GetDirectoryName(e.FullPath);
// 如果文件名以PASSED结尾,则为OK文件
bool isOK = fileName.EndsWith("PASSED");
// 读取文件内容
string content = File.ReadAllText(e.FullPath);
// 从文件名中获取条码
string barcode = fileName.Substring(0, 27);
// 获取建立时间和结束时间
DateTime createTime = File.GetCreationTime(e.FullPath);
DateTime endTime = File.GetLastWriteTime(e.FullPath);
// 获取烧录结果
string result = isOK ? "OK" : "NG";
// 更新计数器和测试结果列表
if (isOK)
{
okCount++;
}
else
{
ngCount++;
}
totalCount++;
passRate = (double)okCount / totalCount;
testResults.Add(new TestResult()
{
Index = folderIndex,
Barcode = barcode,
CreateTime = createTime,
EndTime = endTime,
Result = result
});
// 更新文本框显示
textBoxOKCount.Invoke((MethodInvoker)(() => textBoxOKCount.Text = okCount.ToString()));
textBoxNGCount.Invoke((MethodInvoker)(() => textBoxNGCount.Text = ngCount.ToString()));
textBoxTotalCount.Invoke((MethodInvoker)(() => textBoxTotalCount.Text = totalCount.ToString()));
textBoxPassRate.Invoke((MethodInvoker)(() => textBoxPassRate.Text = passRate.ToString("P")));
// 更新DataGridView
dataGridView.Invoke((MethodInvoker)(() => dataGridView.Refresh()));
// 转换为CSV格式并保存到另一个文件夹
string csvFileName = $"{fileName}.csv";
string csvFilePath = Path.Combine(Properties.Settings.Default.OutputFolderPath, csvFileName);
string csvContent = $"Index,Barcode,CreateTime,EndTime,Result{Environment.NewLine}";
foreach (TestResult testResult in testResults)
{
csvContent += $"{testResult.Index},{testResult.Barcode},{testResult.CreateTime},{testResult.EndTime},{testResult.Result}{Environment.NewLine}";
}
File.WriteAllText(csvFilePath, csvContent);
// 更新文件监视器设置
Properties.Settings.Default.OKCount = okCount;
Properties.Settings.Default.NGCount = ngCount;
Properties.Settings.Default.FolderIndex = folderIndex + 1;
Properties.Settings.Default.Save();
folderIndex++;
watcher.Path = directory;
watcher.Filter = "*.txt";
}
}
public class TestResult
{
public int Index { get; set; }
public string Barcode { get; set; }
public DateTime CreateTime { get; set; }
public DateTime EndTime { get; set; }
public string Result { get; set; }
public Color Color
{
get
{
return Result == "OK" ? Color.Green : Color.Red;
}
}
}
```
需要注意的是,由于文件监视器会在新线程中触发事件,所以需要使用`Invoke`方法在主线程中更新UI组件。另外,为了保存测试数据,可以使用应用程序设置。在`Form1_Load`方法中初始化计数器和测试结果列表,并在`Watcher_Created`方法中更新设置值。
阅读全文