分别跟踪一下shen.Length
和this.textBox1.Text.Length的值,看是否两者相等成立。
和this.textBox1.Text.Length的值,看是否两者相等成立。
解决方案 »
- 如何判断一个整数被10除后还是整数?
- c# 画完折线图有什么方法保存到Powerpoint里面去
- 怎么使datagridview可见行填充整个datagridview
- 在ToolBar控件里有多个用户按钮,想点击按钮时,在Panel里显示用户控件出现问题
- 网络中断时怎么处理?谢谢
- 如何动态显示图片而网页面不进行刷新操作?
- 如何能获得VS窗口中各控件的ID???
- 散分:欢迎大家来讨论
- DataGrid
- 请教高手:在aspx页面中的控件事件如何与cs页面中的事件相关联?
- 怎么样才能把返回的object类型转化成int类型阿?
- for(i=pointIndex;i>0;i--)result/=10;for(i=pointIndex;i>0;i--,result/=10);区别在那?
if(1==1)
{
for(int i = 0;i<3;i++)
{
if(i ==2)
{
break;
}
}
}
else
{
MessageBox.Show("kk");
}
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类有关吧
定义了一个整形值
int len=shen.Length;
它的值始终是10,,也就是你初始化byte[] ok = new Byte[10];
的长度;
改为byte[] ok = new Byte[(int)FileReader.Length];
问题出在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)
{
}
}
}