首页 文章

C#help无法找到路径(LOG IN SYSTEM)

提问于
浏览
-1

所以我的程序运行良好,但每当我在表格2上注册时,它说它找不到路径我不知道什么是错的请帮助我需要稍后传递它,我不知道我是否需要制作C上的新文件夹:只是为了获取LOGIN.ID

{public partial class Form1:Form {public string username,password; public Form1(){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();
        }


    }
}

}

2 回答

  • 0

    您在代码中的许多地方错过了 C 驱动器之后的 :

    var sw = new System.IO.StreamWriter("C\\" + textBox1.Text + "\\login.ID");
    

    改成

    var sw = new System.IO.StreamWriter("C:\\" + textBox1.Text + "\\login.ID");
    
  • 0

    你应该仔细看看你的路径 . 我不认为c \存在 . 可能你正在使用C:\如果你仍然有问题,可能你的TextBox1.Text返回一个错误的路径 .

相关问题