尝试读取二进制文件以连接我网站上的用户

Trying to read binary file to connect with a user on my website

我之前已经能够使用我的 C# 应用程序 (webforms) 从 bin 文件中写入和读取。我做了一些更改后,我仍然可以写入但不能读取超过一个用户。

我用了调试器。即使文件中有 2 个用户,foreach 循环也只计算 Serialization.Users 中的 1 个用户。它是一个静态列表。它以前有效。我不知道我做错了什么。

        ``` 
        format = new BinaryFormatter();
        Users = new List<User>();

        try
        {
            flux = new FileStream(path, FileMode.Open, FileAccess.Read);
        }
        catch
        { 
            Users.Add(new User("root", "root123", "email@abc.com", "Administrator"));
            Users.Add(new User("user", "user123", "email@abc.com", "User"));
            Save(path);
        }
        finally
        {
            Users = (List<User>)format.Deserialize(flux);
            if (flux != null)
               flux.Close();
        } 

        foreach (User account in Serialization.Users)
        {
            if (account.Username == username.Text && account.Password == password.Text)
            return true;
        }
        return false;
        ```

我应该可以用第二个用户登录,但正如我提到的,foreach 循环在 1 次迭代后停止并且 returns 错误。我不明白为什么。

我发现我的代码出了什么问题。

问题不是我的try/catch而是新用户创建他的帐户时出现的。正确登录并通过断开连接返回主页后,Page_Load() 没有再次读取我的二进制文件,因为我忘记在那里反序列化。