c#将测试数据(pass,fail,合格率,总数)保存到本地,下此开启软件,数据显示到相应的组件中,关闭软件时,数据更新,再次开启软件,显示更新后的数据
时间: 2024-05-08 16:21:55 浏览: 18
您可以使用C#中的文件读写操作来实现将测试数据保存到本地的操作。具体步骤如下:
1. 在程序中定义一个类来存储测试数据,例如:
```csharp
class TestData
{
public int Pass { get; set; }
public int Fail { get; set; }
public double QualificationRate { get; set; }
public int Total { get; set; }
}
```
2. 在程序中使用文件读写操作来读取和保存测试数据。例如:
```csharp
// 读取数据
string path = "testdata.txt";
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
string json = sr.ReadToEnd();
TestData testData = JsonConvert.DeserializeObject<TestData>(json);
// 在界面上显示读取的数据
textBox1.Text = testData.Pass.ToString();
textBox2.Text = testData.Fail.ToString();
textBox3.Text = testData.QualificationRate.ToString();
textBox4.Text = testData.Total.ToString();
}
}
// 保存数据
TestData testData = new TestData();
testData.Pass = int.Parse(textBox1.Text);
testData.Fail = int.Parse(textBox2.Text);
testData.QualificationRate = double.Parse(textBox3.Text);
testData.Total = int.Parse(textBox4.Text);
string json = JsonConvert.SerializeObject(testData);
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(json);
}
```
3. 在程序中使用窗体事件来在窗体加载时显示保存的测试数据,并在窗体关闭时保存更新后的测试数据。例如:
```csharp
private void Form1_Load(object sender, EventArgs e)
{
// 在窗体加载时显示保存的数据
string path = "testdata.txt";
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
string json = sr.ReadToEnd();
TestData testData = JsonConvert.DeserializeObject<TestData>(json);
textBox1.Text = testData.Pass.ToString();
textBox2.Text = testData.Fail.ToString();
textBox3.Text = testData.QualificationRate.ToString();
textBox4.Text = testData.Total.ToString();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 在窗体关闭时保存更新后的数据
TestData testData = new TestData();
testData.Pass = int.Parse(textBox1.Text);
testData.Fail = int.Parse(textBox2.Text);
testData.QualificationRate = double.Parse(textBox3.Text);
testData.Total = int.Parse(textBox4.Text);
string json = JsonConvert.SerializeObject(testData);
string path = "testdata.txt";
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(json);
}
}
```
这样就可以实现将测试数据保存到本地,下次开启软件时显示保存的数据,并在关闭软件时保存更新后的数据的操作了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)