IDE是Visual Studio 2010。
我有两个文本文件,分别称为total-cost.txt和amount.txt里面的文件如下图所示:
total-cost.txt
4500000
amount.txt
600
第一个文本文件(total-cost.txt)表示将在文本框中显示的总费用(文本框名称为totalcost)。
第二个文件(amount.txt)表示每秒的增量值。
我正在尝试显示total-cost.txt中的总费用,并每秒钟自动增加value.txt中设置的值
例如:
1秒后的4500000在2秒后变为4500600 4501200,依此类推。
如果我将amount.txt的值从600更改为700,则会变成
1秒后的4500000变为2秒后的4500400变为4500700,依此类推。
该值将使其刷新并仅显示最新的总费用。
问题是我已经在文本框中显示了总成本值,但是我不知道如何增加由amount.txt设置的值
我完成的编码在下面
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.Globalization;
namespace new_countdown
{
public partial class Form1 : Form
{
private string TotalCost;
private int TotalFont;
public Form1()
{
InitializeComponent();
}
private void ReadTotalCostFile()
{
try
{
StreamReader sr = File.OpenText("total-cost.txt");
TotalCost = sr.ReadToEnd();
sr.Close();
}
catch { }
}
private void UpdateDisplay()
{
if (totalcost.Text != TotalCost)
{
totalcost.Text = TotalCost;
}
if (totalcost.Font.Size != TotalFont && TotalFont != 0)
{
this.totalcost.Font = new System.Drawing.Font("Microsoft Sans Serif",(float)TotalFont,System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,((byte)(0)));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateDisplay();
ReadTotalCostFile();
}
}
}
以某种方式,我刚刚完成了文本框中的显示总费用。
我不知道要进行自动递增。
任何人都可以分享想法或解决方案。我非常感谢。
using System;
using System.IO;
private void IncrementInt32ValueInFile(string filePath)
{
var currentFileText = File.ReadAllText(filePath);
if (int.TryParse(currentFileText, out int integerValue))
{
File.WriteAllText(filePath, Convert.ToString(++integerValue));
}
throw new Exception($"Incorrect file content. Path: {filePath}"); // If value in file can't be parsed as integer
}