StreamReader din1 = File.OpenText(tempPath+"\\pwd.dat");
String str1;

while ((str1 = din1.ReadLine()) != null) 
{
i++;
} StreamReader din = File.OpenText(tempPath+"\\pwd.dat");
String str;
Basenames = new string[i];
Basepwds = new string[i];
int j = 0;
while ((str = din.ReadLine()) != null) 
{
string[] split = str.Split(new char[]{'-'});

Basenames[j] = split[0];
Basepwds[j] = split[1];
j++; } StreamReader aa = File.OpenText(tempPath+"\\login.dat");
String strl;
Logname = new string[i];
permissions = new string[i];
int m = 0;
while ((strl = aa.ReadLine()) != null) 
{
string[] splits = strl.Split(new char[]{'-'});

Basenames[m] = splits[0];
Basepwds[m] = splits[1];
m++; }
private void button1_Click(object sender, System.EventArgs e)
{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
string MD5name = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(textBox1.Text)), 4,8); 
string MD5pwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(textBox2.Text)), 4, 8);
MD5name = MD5name.Replace("-", ""); 
MD5pwd = MD5pwd.Replace("-", "");
for(int k = 0;k<i;k++)
{
if(MD5name==Basenames[k] && MD5pwd==Basepwds[k]) {
this.Hide();
MainForm mFrom=new MainForm();   
mFrom.Show();

}
else
{
label4.Visible = true;
label4.Text = "用户名或密码错误!";
textBox1.Text = "";
textBox2.Text = "";
}

}

}
上面是一小段登陆的代码,每次登录都去读配置文件,pwd.dat和login.dat文件,最早只读pwd.dat,里面就是放用户名密码,现在我要加一个登陆权限验证,每次登录在去读login.dat文件,里面放的是用户名和权限级,比如1,2,3. 现在这里if(MD5name==Basenames[k] && MD5pwd==Basepwds[k]) {
this.Hide();
MainForm mFrom=new MainForm();   
mFrom.Show();判断后就直接显示登陆后的窗口了,我想在把我读出来的那个权限级传过去,在MainForm拿这个权限级,然后在里面去判断,这个权限级应该显示的是什么。可我不知道怎么把这参数传过去,我是做java的,c#不太懂,谁能帮我看看,谢谢了

解决方案 »

  1.   

    这是详细的两个代码
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    namespace Sensky.Resource.WForm
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Login : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public static int i;

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2; string[] Basenames =null;
    private System.Windows.Forms.Label label4;
    string[] Basepwds = null; string[] Logname =null;
    string[] permissions= null;//权限
    int permission; public Login()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    {
    String tempPath = getAbsPath();
     
    StreamReader din1 = File.OpenText(tempPath+"\\pwd.dat");
    String str1;

    while ((str1 = din1.ReadLine()) != null) 
    {
    i++;
    } StreamReader din = File.OpenText(tempPath+"\\pwd.dat");
    String str;
    Basenames = new string[i];
    Basepwds = new string[i];
    int j = 0;
    while ((str = din.ReadLine()) != null) 
    {
    string[] split = str.Split(new char[]{'-'});

    Basenames[j] = split[0];
    Basepwds[j] = split[1];
    j++; } StreamReader aa = File.OpenText(tempPath+"\\login.dat");
    String strl;
    Logname = new string[i];
    permissions = new string[i];
    int m = 0;
    while ((strl = aa.ReadLine()) != null) 
    {
    string[] splits = strl.Split(new char[]{'-'});

    Basenames[m] = splits[0];
    Basepwds[m] = splits[1];
    m++; } }
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } static String getAbsPath ( ) 
    {
    return Environment.CurrentDirectory;
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.label4 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(40, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(168, 24);
    this.label1.TabIndex = 0;
    this.label1.Text = "请您输入用户名和密码";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(32, 40);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(56, 24);
    this.label2.TabIndex = 1;
    this.label2.Text = "用户名";
    this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(32, 80);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(56, 24);
    this.label3.TabIndex = 2;
    this.label3.Text = "密码";
    this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(112, 40);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(112, 21);
    this.textBox1.TabIndex = 3;
    this.textBox1.Text = "";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(112, 80);
    this.textBox2.Name = "textBox2";
    this.textBox2.PasswordChar = '*';
    this.textBox2.Size = new System.Drawing.Size(112, 21);
    this.textBox2.TabIndex = 4;
    this.textBox2.Text = "";
    // 
    // button1
    // 
    this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    this.button1.Location = new System.Drawing.Point(24, 120);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(64, 24);
    this.button1.TabIndex = 5;
    this.button1.Text = "OK";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    this.button2.Location = new System.Drawing.Point(160, 120);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(64, 24);
    this.button2.TabIndex = 6;
    this.button2.Text = "CANCEL";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // label4
    // 
    this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.label4.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
    this.label4.Location = new System.Drawing.Point(40, 8);
    this.label4.Name = "label4";
    this.label4.Size = new System.Drawing.Size(184, 24);
    this.label4.TabIndex = 7;
    this.label4.Text = "label4";
    this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    this.label4.Visible = false;
    // 
    // Login
    // 
    this.AcceptButton = this.button1;
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.CancelButton = this.button2;
    this.ClientSize = new System.Drawing.Size(248, 158);
    this.Controls.Add(this.label4);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Name = "Login";
    this.Text = "登陆";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary> private void button1_Click(object sender, System.EventArgs e)
    {

    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
    string MD5name = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(textBox1.Text)), 4, 8); 
    string MD5pwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(textBox2.Text)), 4, 8);
    MD5name = MD5name.Replace("-", ""); 
    MD5pwd = MD5pwd.Replace("-", "");
    for(int k = 0;k<i;k++)
    {
    if(MD5name==Basenames[k] && MD5pwd==Basepwds[k] && MD5name==Logname[k])
    {
    this.Hide();
    MainForm mFrom=new MainForm();   
    mFrom.Show(permissions[k]);

    }
    else
    {
    label4.Visible = true;
    label4.Text = "用户名或密码错误!";
    textBox1.Text = "";
    textBox2.Text = "";
    }

    }

    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    Dispose();
    }
    }
    }
      

  2.   

    页面传值得问题,看看这个
    http://www.cnblogs.com/virusswb/archive/2008/03/31/1131276.html
      

  3.   

    重载个Mainform的构造方法不行么?
    或者在Mainform里面写个set函数,new完了,就set权限
    再或者Loginform里面写个get函数,Mainform主动过来取只需要控制好set值,和使用值的先后顺序就行了