【发布时间】:2018-06-20 11:17:45
【问题描述】:
'未处理的异常:
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());
}
}
【问题讨论】:
-
当前目录可能有误。
-
将 .txt 文件放在可执行文件旁边,并确保从该目录运行应用程序
-
将 Logins.txt 文件属性更改为 Embedded Resource
-
在解决方案资源管理器中右键单击 txt 文件,然后单击属性。在此之后,将您的构建配置更改为嵌入式资源。
-
已经尝试使用可执行文件旁边的 txt 来填充路径,使用 @"C:\---pathing---\AppAttempt\Logins.txt" 和 "Logins.txt"没有成功。嵌入式资源? (编辑:已将 txt 更改为嵌入式资源。这是做什么的?)
标签: c# xamarin.forms streamreader