猿问

找不到 txt 文件 StreamReader

'未处理的异常:


System.IO.FileNotFoundException: 无法找到文件“/Logins.txt”发生'


嗨,新手在 xamarin c# 中制作了一个非常基本的登录/注册系统,它使用来自 Systsem.IO 的 StreamReader 读取带有存储的用户名和密码的文本文件。txt 文件与 .cs 文件本身位于同一目录中,并且在解决方案资源管理器中可见。我试过输入完整路径但没有成功,并以管理员身份运行,以防万一它与权限有关。有什么不对吗?


public partial class LoginPage : ContentPage

{

    public LoginPage()

    {

        InitializeComponent();

    }

    List<string> user = new List<string>();

    List<string> pass = new List<string>();


    public void btnLogin_Clicked(object sender, EventArgs e)

    {

        //Read the txt

        StreamReader sr = new StreamReader("Logins.txt");

        string line = "";


        //Read every line until there is nothing in the next line

        while ((line = sr.ReadLine()) != null)

        {

            //Grab items within new lines separated by a space and chuck them into their array

            string[] components = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            user.Add(components[0]);

            pass.Add(components[0]);

        }

        //Check entries are on the same line and match

        if (user.Contains(txtUsername.Text) && pass.Contains(txtPassword.Text) && Array.IndexOf(user.ToArray(), txtUsername.Text) == Array.IndexOf(pass.ToArray(), txtPassword.Text))

        {

            Navigation.PushModalAsync(new HomeScreen());

        }

        else

            DisplayAlert("Error", "The username or password you have entered is incorrect", "Ok");

    }

    private void Cancel_Clicked(object sender, EventArgs e)

    {

        Navigation.PushModalAsync(new MainPage());

    }

}


至尊宝的传说
浏览 319回答 1
1回答

qq_遁去的一_1

如果将Logins.txt文件更改为嵌入资源,则可以在运行时从程序集中加载该资源,如下所示:var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoginPage)).Assembly;Stream stream = assembly.GetManifestResourceStream("YourAssembly.Logins.txt");string text = "";using (var reader = new System.IO.StreamReader (stream))&nbsp;{&nbsp; &nbsp; text = reader.ReadToEnd();}YouAssembly指的是构建项目时将生成的程序集 (dll) 的名称。如果你不知道这是什么,或者没有改变它,它可能与项目同名。Xamarin 文档有一个很好的例子来说明这是如何完成的:Xamarin.Forms 中的文件处理编辑:在构造函数中执行此操作并处理内容以便您可以进行身份验证处理的更完整示例。但首先,让我们定义一个 POCO 来保存用户名/密码的详细信息:public class UserCredentials{&nbsp; &nbsp; public string Username {get;set;}&nbsp; &nbsp; public string Password {get;set;}}添加一个属性来保存这个类:List<UserCredentials> CredentialList {get;set;}现在,在构造函数中:public LoginPage(){&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; // Read in the content of the embedded resource, but let's read&nbsp;&nbsp; &nbsp; // each line to make processing a little easier&nbsp; &nbsp; var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoginPage)).Assembly;&nbsp; &nbsp; Stream stream = assembly.GetManifestResourceStream("YourAssembly.Logins.txt");&nbsp; &nbsp; var textLines = new List<string>();&nbsp; &nbsp; string line = null;&nbsp; &nbsp; using (var reader = new System.IO.StreamReader (stream))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while ((line = sr.ReadLine()) != null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textLines.Add(line);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Init our cred list&nbsp; &nbsp; this.CredentialList = new List<UserCredentials>();&nbsp; &nbsp; // Read out the contents of the username/password combinations&nbsp; &nbsp; foreach (var line in textLines)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Grab items within new lines separated by a space and chuck them into their array&nbsp; &nbsp; &nbsp; &nbsp; string[] components = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);&nbsp; &nbsp; &nbsp; &nbsp; // Add the combination to our list&nbsp; &nbsp; &nbsp; &nbsp; this.CredentialList.Add(new UserCredential&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Username = user.Add(components[0]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password = pass.Add(components[0])&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}现在我们的登录变得容易多了。以前,我们首先检查用户名/密码是否都存在于我们的已知数据集中,然后检查它们在两个列表中是否位于相同的索引处。public void btnLogin_Clicked(object sender, EventArgs e){&nbsp; &nbsp; // Now can use linq to validate the user&nbsp; &nbsp; // NOTE: this is case sensitive&nbsp; &nbsp; if (this.CredentialList.Any(a => a.Username == txtUsername.Text && a.Password == txtPassword.Text))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // NOTE: you've only validated the user here, but aren't passing&nbsp; &nbsp; &nbsp; &nbsp; // or storing any detail of who the currently logged in user is&nbsp; &nbsp; &nbsp; &nbsp; Navigation.PushModalAsync(new HomeScreen());&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DisplayAlert("Error", "The username or password you have entered is incorrect", "Ok");&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答