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-29 21:04:54 浏览: 58
以下是一个示例代码,仅供参考:
首先,需要在窗体的Load事件中添加开机自启代码:
```
private void Form1_Load(object sender, EventArgs e)
{
// 设置开机自启
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApp", Application.ExecutablePath);
}
```
然后,在“开启ICT测试”按钮的Click事件中,扫描数据库是否存在设置的良品或不良品条码的当天测试记录:
```
private void btnStart_Click(object sender, EventArgs e)
{
// 扫描数据库是否有当天的测试记录
string today = DateTime.Now.ToString("yyyy-MM-dd");
bool hasRecord = CheckDatabase(today);
if (hasRecord)
{
// 如果有记录,则提示已点检
lblStatus.Text = "已点检,请进行ICT测试";
lblStatus.ForeColor = Color.Green;
}
else
{
// 如果没有记录,则提示请进行测试前点检
lblStatus.Text = "请进行测试前点检";
lblStatus.ForeColor = Color.Red;
}
}
```
CheckDatabase方法用于扫描数据库,判断是否存在设置的良品或不良品条码的当天测试记录:
```
private bool CheckDatabase(string date)
{
bool hasRecord = false;
string connStr = "数据库连接字符串";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "SELECT COUNT(*) FROM ICT_test WHERE Date=@date AND Barcode IN (SELECT Barcode FROM GoodBarcodes UNION SELECT Barcode FROM BadBarcodes)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@date", date);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
hasRecord = true;
}
}
return hasRecord;
}
```
接着,在“打开ICT测试”按钮的Click事件中,判断输入的条码是否为设置的良品或不良品条码:
```
private void btnOpenICT_Click(object sender, EventArgs e)
{
// 判断输入的条码是否为设置的良品或不良品条码
string barcode = txtBarcode.Text.Trim();
bool isGood = CheckGoodBarcode(barcode);
bool isBad = CheckBadBarcode(barcode);
if (!isGood && !isBad)
{
// 如果不是设置的良品或不良品条码,则提示条码错误
lblStatus.Text = "条码错误";
lblStatus.ForeColor = Color.Red;
return;
}
if (isGood)
{
// 如果是设置的良品条码,则提示请进行良品点检
lblStatus.Text = "请进行良品点检";
lblStatus.ForeColor = Color.Green;
}
else if (isBad)
{
// 如果是设置的不良品条码,则提示请进行不良品点检
lblStatus.Text = "请进行不良品点检";
lblStatus.ForeColor = Color.Green;
}
// 打开ICT测试软件
OpenICTSoftware();
}
```
CheckGoodBarcode和CheckBadBarcode方法用于判断输入的条码是否为设置的良品或不良品条码:
```
private bool CheckGoodBarcode(string barcode)
{
bool isGood = false;
string filePath = "良品条码文件路径";
string[] goodBarcodes = File.ReadAllLines(filePath);
foreach (string goodBarcode in goodBarcodes)
{
if (goodBarcode.Trim() == barcode)
{
isGood = true;
break;
}
}
return isGood;
}
private bool CheckBadBarcode(string barcode)
{
bool isBad = false;
string filePath = "不良品条码文件路径";
string[] badBarcodes = File.ReadAllLines(filePath);
foreach (string badBarcode in badBarcodes)
{
if (badBarcode.Trim() == barcode)
{
isBad = true;
break;
}
}
return isBad;
}
```
最后,在ICT测试完成后,需要将测试记录上传到数据库:
```
private void UploadRecord(string barcode, string date, string result)
{
string connStr = "数据库连接字符串";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "INSERT INTO ICT_test (Barcode, Date, Result) VALUES (@barcode, @date, @result)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@barcode", barcode);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@result", result);
cmd.ExecuteNonQuery();
}
}
```
完整的代码:
```
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 设置开机自启
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApp", Application.ExecutablePath);
}
private void btnStart_Click(object sender, EventArgs e)
{
// 扫描数据库是否有当天的测试记录
string today = DateTime.Now.ToString("yyyy-MM-dd");
bool hasRecord = CheckDatabase(today);
if (hasRecord)
{
// 如果有记录,则提示已点检
lblStatus.Text = "已点检,请进行ICT测试";
lblStatus.ForeColor = Color.Green;
}
else
{
// 如果没有记录,则提示请进行测试前点检
lblStatus.Text = "请进行测试前点检";
lblStatus.ForeColor = Color.Red;
}
}
private bool CheckDatabase(string date)
{
bool hasRecord = false;
string connStr = "数据库连接字符串";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "SELECT COUNT(*) FROM ICT_test WHERE Date=@date AND Barcode IN (SELECT Barcode FROM GoodBarcodes UNION SELECT Barcode FROM BadBarcodes)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@date", date);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
hasRecord = true;
}
}
return hasRecord;
}
private void btnOpenICT_Click(object sender, EventArgs e)
{
// 判断输入的条码是否为设置的良品或不良品条码
string barcode = txtBarcode.Text.Trim();
bool isGood = CheckGoodBarcode(barcode);
bool isBad = CheckBadBarcode(barcode);
if (!isGood && !isBad)
{
// 如果不是设置的良品或不良品条码,则提示条码错误
lblStatus.Text = "条码错误";
lblStatus.ForeColor = Color.Red;
return;
}
if (isGood)
{
// 如果是设置的良品条码,则提示请进行良品点检
lblStatus.Text = "请进行良品点检";
lblStatus.ForeColor = Color.Green;
}
else if (isBad)
{
// 如果是设置的不良品条码,则提示请进行不良品点检
lblStatus.Text = "请进行不良品点检";
lblStatus.ForeColor = Color.Green;
}
// 打开ICT测试软件
OpenICTSoftware();
}
private bool CheckGoodBarcode(string barcode)
{
bool isGood = false;
string filePath = "良品条码文件路径";
string[] goodBarcodes = File.ReadAllLines(filePath);
foreach (string goodBarcode in goodBarcodes)
{
if (goodBarcode.Trim() == barcode)
{
isGood = true;
break;
}
}
return isGood;
}
private bool CheckBadBarcode(string barcode)
{
bool isBad = false;
string filePath = "不良品条码文件路径";
string[] badBarcodes = File.ReadAllLines(filePath);
foreach (string badBarcode in badBarcodes)
{
if (badBarcode.Trim() == barcode)
{
isBad = true;
break;
}
}
return isBad;
}
private void OpenICTSoftware()
{
// 打开ICT测试软件
string softwarePath = "ICT测试软件路径";
Process.Start(softwarePath);
}
private void btnPass_Click(object sender, EventArgs e)
{
// 良品点检完成,上传测试记录
string barcode = txtBarcode.Text.Trim();
string date = DateTime.Now.ToString("yyyy-MM-dd");
string result = "Pass";
UploadRecord(barcode, date, result);
// 提示良品点检完成
lblStatus.Text = "良品点检完成";
lblStatus.ForeColor = Color.Green;
}
private void btnFail_Click(object sender, EventArgs e)
{
// 不良品点检完成,上传测试记录
string barcode = txtBarcode.Text.Trim();
string date = DateTime.Now.ToString("yyyy-MM-dd");
string result = "Fail";
UploadRecord(barcode, date, result);
// 提示不良品点检完成
lblStatus.Text = "不良品点检完成";
lblStatus.ForeColor = Color.Green;
}
private void UploadRecord(string barcode, string date, string result)
{
string connStr = "数据库连接字符串";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "INSERT INTO ICT_test (Barcode, Date, Result) VALUES (@barcode, @date, @result)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@barcode", barcode);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@result", result);
cmd.ExecuteNonQuery();
}
}
}
```
阅读全文