简体   繁体   English

C# Streamreader 问题

[英]C# Streamreader problems

I'm building a program that has you input a number and remembers it when you re-open the program.我正在构建一个程序,让您输入一个数字并在您重新打开该程序时记住它。 It uses a text file to save the number in. I'm using streamreader to read the text file to get the number you entered last, but it always throws an exception.它使用一个文本文件来保存数字。我使用streamreader 读取文本文件以获取您最后输入的数字,但它总是抛出异常。 Where should I put the text file or change my code so it can read and edit the text?我应该在哪里放置文本文件或更改我的代码,以便它可以读取和编辑文本? Here is my code:这是我的代码:

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)
        {

        }
    }

} }

To answer your question:回答你的问题:

Where should I put the text file...?我应该把文本文件放在哪里...?

You haven't specified a path to cookies.txt so the program will look for it in the same directory where it's running.您尚未指定 cookies.txt 的路径,因此程序将在它运行的同一目录中查找它。 If you change cookies.txt to include a path, for example C:\\dev\\cookies.txt , then you can store the file wherever you like.如果您更改cookies.txt以包含路径,例如C:\\dev\\cookies.txt ,那么您可以将文件存储在您喜欢的任何位置。

That will allow you to get past the file not found error and address any other problems you have in there.这将允许您通过文件未找到错误并解决您在那里遇到的任何其他问题。

Every time it says "Your cookie text file is missing!"每次它说“您的 cookie 文本文件丢失!”

Problem 1: You are not specifying proper path of your input file.问题 1:您没有指定输入文件的正确路径。

Solution 1: You need to get the Currentpath of your application from where it is running and then combine it with the filename using Path.Combine() method.解决方案 1:您需要从应用程序运行的位置获取应用程序的 Currentpath,然后使用Path.Combine()方法将其与文件名组合。

Try This:尝试这个:

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"cookies.txt");
using (StreamReader sr = new StreamReader(path))
{
    int data = Convert.ToInt16(sr.ReadToEnd());
    tb.Text = Convert.ToString(data + 1);
}

Suggestion : You need to always display the Error message in Catch block to identify the problem.建议:您需要始终在 Catch 块中显示错误消息以识别问题。

You can call ToString() on Exception object to get the complete exception info.您可以在 Exception 对象上调用ToString()以获取完整的异常信息。

catch (Exception ex) 
{
    MessageBox.Show(ex.ToSTring(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM