分别跟踪一下shen.Length
和this.textBox1.Text.Length的值,看是否两者相等成立。

解决方案 »

  1.   

    不会呀,你看如下的代码:
    if(1==1)
    {
    for(int i = 0;i<3;i++)
    {

    if(i ==2)
    {

    break;
    }

    }
    }
    else
    {
    MessageBox.Show("kk");
    }
      

  2.   

    我跟踪过,当shen.Length和this.textBox1.Text.Length相等时却执行else后的语句,奇怪!!!!
      

  3.   

    如果把
    System.IO.FileStream FileReader = System.IO.File.Open("Password.txt",System.IO.FileMode.Open);
    byte[] ok = new Byte[10];
    FileReader.Read(ok,0,10);
    FileReader.Close();
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    string shen = (encoding.GetString(ok));
    删掉用string shen = "aaaa";这句就正常,我想可能与System.Text.Encoding类有关吧
      

  4.   

    把textBox1.Text用和shen相同的编码编一下再比比看。(是不是有中文?)
      

  5.   

    我测试了一下
    定义了一个整形值
    int len=shen.Length;
    它的值始终是10,,也就是你初始化byte[] ok = new Byte[10];
    的长度;
    改为byte[] ok = new Byte[(int)FileReader.Length];
      

  6.   


     问题出在byte[] ok = new Byte[10];using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Text;namespace WindowsApplication6
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(72, 56);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(144, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(16, 104);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(104, 32);
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(160, 104);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(104, 32);
    this.button2.TabIndex = 1;
    this.button2.Text = "button1";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 157);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button2);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    //
    System.IO.FileStream FileReader = System.IO.File.Open("Password.txt",System.IO.FileMode.Open);
    byte[] ok = new Byte[FileReader.Length];
    FileReader.Read(ok,0,ok.Length);
    FileReader.Close();
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    string shen = encoding.GetString(ok).Trim();
    //string shen = "aaaa"; bool Same = true;
    if(shen.Length==this.textBox1.Text.Length)//请注意些处,当条件为真时,却执行else后的语句
    {
    for(int i = 0;i<shen.Length;i++)
    {

    if(shen[i]!=this.textBox1.Text[i])
    {
    Same = false;
    break;
    }

    }
    }
    else
    {
    Same = false;
    }
    if(Same)
    {
    MessageBox.Show("the Password is right","Password");

    this.Close();
    }
    else
    {
    MessageBox.Show("the Password is wrong,Please try again","Password");
    }
    //}

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

    }
    }
    }