C#无法';找不到路径(登录系统)
本文关键字:登录 系统 路径 找不到 无法 | 更新日期: 2023-09-27 17:59:45
所以我的程序运行得很好,但每当我在Form 2上注册时,它都说找不到路径。我不知道出了什么问题,请帮我稍后传递,我不知道是否需要在C:上创建一个新文件夹来获取LOGIN.ID
{公共分部类Form1:Form{公共字符串用户名、密码;公共窗体1(){InitializeComponent();}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var sr = new System.IO.StreamReader("C''" + textBox1.Text + "''login.ID");
username = sr.ReadLine();
password = sr.ReadLine();
sr.Close();
if (username == textBox1.Text && password == textBox2.Text)
MessageBox.Show("Log-in Successfull", "Success!");
else
MessageBox.Show("Username or password is wrong! ","Error!");
}
catch (System.IO.DirectoryNotFoundException )
{
MessageBox.Show("The user doesn't exist!", "Error!");
}
}
}
}
//形式2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var sw = new System.IO.StreamWriter ("C''" + textBox1.Text + "''login.ID");
sw.Write(textBox1.Text + "'n" + textBox2.Text);
sw.Close();
}
catch(System.IO.DriveNotFoundException )
{
System.IO.Directory.CreateDirectory("C:''" + textBox1.Text);
var sw = new System.IO.StreamWriter("C''" + textBox1.Text + "''login.ID");
sw.Write(textBox1.Text + "'n" + textBox2.Text);
sw.Close();
}
}
}
}
在代码中,C
驱动器后面的许多地方都缺少:
var sw = new System.IO.StreamWriter("C''" + textBox1.Text + "''login.ID");
更改为
var sw = new System.IO.StreamWriter("C:''" + textBox1.Text + "''login.ID");
您应该仔细查看您的路径。我认为c不存在。可能您正在使用C:''如果你仍然有问题,也许你的TextBox1.Text返回了一个错误的路径。