C#帮助无法';找不到路径(登录系统)
所以我的程序运行得很好,但每当我在表单2上注册时,它会说它找不到路径,我不知道出了什么问题,请帮助我,我需要稍后通过它,我不知道我是否需要在C:上创建一个新文件夹来获取LOGIN.ID { 公共部分类Form1:Form { 公共字符串用户名、密码; 公共表格1() { 初始化组件(); }C#帮助无法';找不到路径(登录系统),c#,path,C#,Path,所以我的程序运行得很好,但每当我在表单2上注册时,它会说它找不到路径,我不知道出了什么问题,请帮助我,我需要稍后通过它,我不知道我是否需要在C:上创建一个新文件夹来获取LOGIN.ID { 公共部分类Form1:Form { 公共字符串用户名、密码; 公共表格1() { 初始化组件(); } private void button2_Click(object sender, EventArgs e) { Form2 form2 = new Form2();
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!");
}
}
}
}
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!");
}
}
}
//表格二
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
驱动器之后,您缺少了:
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!");
}
}
}
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返回了错误的路径。您一直在混合使用“C\\”和“C:\\”。什么是“C\\”?这是胡说八道。修复你的路径,使它们始终是“C:\\”。我如何修复它?我是否应该为C创建文件夹:\\Example C\test\Example:@form 1 var sr=new System.IO.StreamReader(“C\test1\”+textBox1.Text+“\\login.ID”);username=sr.ReadLine();密码=sr.ReadLine();高级关闭();停止使用“C\”。这没有任何意义。永远不要再使用它。曾经从来没有。应该是“C:\”。你需要阅读Windows文件路径。谢谢,谢谢,现在没关系了,我刚刚用C:创建了一个新文件夹来更正路径。。对不起,我还没睡,我做了4个程序,因为我需要稍后通过。。谢谢你:)!