2016-08-02 86 views
-1

所以我的程序运行良好,但每当我注册表单2它说它找不到路径我不知道什么是错的请帮助我稍后需要通过它,我不知道我是否需要做一个新的文件夹在C:刚拿到LOGIN.IDC#帮助无法找到路径(登录系统)

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


    } 
} 

}

+0

您继续混合“C \\”和“C:\\”。什么是“C \\”?这是无稽之谈。修复你的路径,使它们始终如一“C:\\”。 – itsme86

+0

我该如何解决它?我应该为C创建一个文件夹:\\ 示例C \ test \ –

+0

示例:@form 1 var sr = new System.IO.StreamReader(“C \ test1 \”+ textBox1.Text +“\\ login.ID “); username = sr.ReadLine(); password = sr.ReadLine(); sr.Close(); –

回答

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中创建了一个新文件夹:纠正路径..即时通讯对不起,我还没有睡觉,我做了4个程序,因为我需要通过它后..谢谢你:)! –

0

你应该把你的路径密切关注。我不认为c \存在。 也许你正在使用C:\ 如果你仍然有问题,也许你的TextBox1.Text返回一个错误的路径。