我正在構建一個程序,輸入一個數字並在您重新打開該程序時記住它。它使用一個文本文件來保存數字。我使用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();}' –