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

时间:2016-08-02 23:16:22

标签: c# path

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

{     公共部分类Form1:表格     {         公共字符串用户名,密码;         公共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 :(得分:0)

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

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

更改为

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

答案 1 :(得分:0)

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