c#窗体应用程序实现软件开机自启,并可以通过路径控制ICT测试软件的打开和关闭。良品条码和不良品条码可以在本地文件夹中的txt文件中获取。当软件开启,定时器就在数据库扫描是否有设置的良品和不良品条码的当天测试记录(数据库ICT_test表中Barcode和Date项的记录),如果存在,lable状态栏变绿并提示“已点检,请进行ICT测试”。就可以通过c#软件开启按钮打开ICT测试软件,进行其他条码的ICT测试。如果数据库中没有设置的良品和不良品条码的测试记录,点击开启ICT测试按钮时,label状态栏变红,并提示“请进行测试前点检”,然后就先进行设置条码的ICT测试,判断扫描到txt_barcode的条码是否为txt文件中设置的良品或不良品条码,如输入的条码不符合,label状态栏变红,并提示“条码错误”,如输入的条码正确并为设置的良品条码,label状态栏变绿,并提示“请进行良品点检”,这时就能打开ICT测试软件,然后将扫到txt_barcode的良品条码通过模拟键盘复制黏贴到ICT条码栏,进行ICT测试,当发现有良品条码的点检报告生成,表示良品点检完成。测试完成后label状态栏提示“良品点检完成”,并将测试记录上传到数据库。c#代码实现
时间: 2023-11-30 16:02:17 浏览: 16
以下是示例代码,仅供参考:
在窗体的Load事件中添加自启动代码:
```
private void Form1_Load(object sender, EventArgs e)
{
// 设置自启动
string appName = "MyApp";
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue(appName, Application.ExecutablePath);
}
```
在窗体中添加打开ICT测试软件和关闭ICT测试软件的方法:
```
private Process ictProcess;
private void OpenICTTest()
{
// 打开ICT测试软件
string ictPath = "C:\\ICTTest\\ICT.exe"; // ICT测试软件路径
ictProcess = Process.Start(ictPath);
}
private void CloseICTTest()
{
// 关闭ICT测试软件
if (ictProcess != null && !ictProcess.HasExited)
{
ictProcess.Kill();
}
}
```
在窗体中添加扫描条码并进行点检的方法:
```
private void DoBarcodeCheck(string barcode)
{
// 判断扫描的条码是否为良品或不良品条码
string goodBarcodesPath = "C:\\Barcodes\\GoodBarcodes.txt"; // 良品条码文件路径
string badBarcodesPath = "C:\\Barcodes\\BadBarcodes.txt"; // 不良品条码文件路径
bool isGoodBarcode = File.ReadLines(goodBarcodesPath).Contains(barcode);
bool isBadBarcode = File.ReadLines(badBarcodesPath).Contains(barcode);
if (!isGoodBarcode && !isBadBarcode)
{
// 条码错误
labelStatus.Text = "条码错误";
labelStatus.ForeColor = Color.Red;
return;
}
if (isGoodBarcode)
{
// 良品点检
labelStatus.Text = "请进行良品点检";
labelStatus.ForeColor = Color.Green;
OpenICTTest();
}
else if (isBadBarcode)
{
// 不良品点检
labelStatus.Text = "请进行不良品点检";
labelStatus.ForeColor = Color.Yellow;
// TODO: 实现不良品点检逻辑
}
}
```
在窗体中添加定时器Tick事件的代码,用于扫描数据库中的测试记录:
```
private void timerScan_Tick(object sender, EventArgs e)
{
// 扫描数据库是否有当天的良品和不良品条码的测试记录
string today = DateTime.Now.ToString("yyyy-MM-dd");
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM ICT_test WHERE Date=@date AND (Barcode=@goodBarcode OR Barcode=@badBarcode)", connection);
command.Parameters.AddWithValue("@date", today);
command.Parameters.AddWithValue("@goodBarcode", "GoodBarcode");
command.Parameters.AddWithValue("@badBarcode", "BadBarcode");
int count = (int)command.ExecuteScalar();
connection.Close();
if (count > 0)
{
// 已点检,请进行ICT测试
labelStatus.Text = "已点检,请进行ICT测试";
labelStatus.ForeColor = Color.Green;
}
else
{
// 请进行测试前点检
labelStatus.Text = "请进行测试前点检";
labelStatus.ForeColor = Color.Red;
}
}
}
```
在窗体中添加开始ICT测试和上传测试记录的方法:
```
private void StartICTTest()
{
// TODO: 实现ICT测试逻辑
// 测试完成后,显示良品点检完成
labelStatus.Text = "良品点检完成";
labelStatus.ForeColor = Color.Green;
// 上传测试记录到数据库
string today = DateTime.Now.ToString("yyyy-MM-dd");
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO ICT_test (Barcode, Date) VALUES (@barcode, @date)", connection);
command.Parameters.AddWithValue("@barcode", "GoodBarcode");
command.Parameters.AddWithValue("@date", today);
command.ExecuteNonQuery();
connection.Close();
}
}
private void btnStartTest_Click(object sender, EventArgs e)
{
if (labelStatus.ForeColor == Color.Red)
{
// 请先进行测试前点检
MessageBox.Show("请先进行测试前点检!");
return;
}
StartICTTest();
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)