请帮助FileStream阅读. [英] please help with FileStream reading.

查看:85
本文介绍了请帮助FileStream阅读.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Login_btn_Click(object sender, EventArgs e)
        {
            User u1 = new User();
            u1.Username = Username_login_Textbox.Text;
            u1.Password = Password_login_Textbox.Text;

            FileStream fs1 = new FileStream(@"C:\Users\QEBO\Desktop\Exam_System_Users\test.txt", FileMode.OpenOrCreate, FileAccess.Write);
            BinaryReader br = new BinaryReader(fs1); // here appears THE STREAM WAS NOT READABLE :(

            if (u1.Username == fs1.Length.ToString())
            {
                if (u1.Password == fs1.Length.ToString())
                {
                    ExamSystem es = new ExamSystem();
                    es.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Invalid username or password");
            }
            fs1.Flush();
            fs1.Close();
            br.Close();
        }

推荐答案

我猜测问题是您打开了要写入的流,然后尝试读取它.

尝试以下方法:
I''m guessing the issue is that you open the stream to write, then attempt to read it.

Try this instead:
FileStream fs1 = new FileStream(@"C:\Users\QEBO\Desktop\Exam_System_Users\test.txt", FileMode.OpenOrCreate, FileAccess.Read);
BinaryReader br = new BinaryReader(fs1);


因abolsutley幼稚的主题行而被否决,而你没有这样做"甚至不问一个问题.在代码中添加注释并不完全是我所说的问题.

但是,从代码片段中,您打开了文件的只写"访问权限,然后尝试读取该文件.与BinaryReader.您看到这个问题了吗?
Downvoted for the abolsutley childish subject line and you didn''t even ask a question. Putting a comment in the code isn''t exactly what I would call a question.

But, from the code snippet, you opened the file for Write only access, then tried to read the file. with a BinaryReader. Do you see a problem with that??


这篇关于请帮助FileStream阅读.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆