我正在构建一个程序,输入一个数字并在您重新打开该程序时记住它。它使用一个文本文件来保存数字。我使用streamreader读取文本文件以获取最后输入的数字,但它总是抛出异常。我应该在哪里放置文本文件或更改我的代码,以便它可以读取和编辑文本?这里是我的代码:C#Streamreader问题
namespace Cookie_Clicker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void tb_TextBox(object sender, RoutedEventArgs e)
{
}
private void add_Click(object sender, RoutedEventArgs e)
{
try
{
using (StreamReader sr = new StreamReader("cookies.txt"))
{
int data = Convert.ToInt16(sr.ReadToEnd());
tb.Text = Convert.ToString(data + 1);
}
}
catch (Exception)
{
MessageBox.Show("Your cookie text file is missing!");
}
}
private void reset_Click(object sender, RoutedEventArgs e)
{
}
}
}
什么问题? – SLaks
保存文件的代码在哪里? – Steve
尝试此catch块 - >'catch(Exception ex){MessageBox.Show(ex.ToSTring();}' –