第一个Winform 程序 (附一个需求实现,望大家帮忙)
我现在的 选择 .pdf 可以筛选出 .pdf的文件,但是选择 .txt之后只会筛选出 .txt文件,如何实现出 选择2个 都可以实现的代码
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form //partial可以使一个程序的代码分别处在不同的文件中
{
string currentDirectory;//用于存路径
string currentextens;//用于存 选择扩展名
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != string.Empty && !Directory.Exists(textBox1.Text))//判断输入为非空 并且 不存在的时弹出信息
{
MessageBox.Show("Invalid Directory","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if (textBox1.Text == string.Empty)
{
currentDirectory = textBox1.Text;
MessageBox.Show("Your Enter is Empty","Error");
}
else if (Directory.Exists(textBox1.Text))
{
textBox2.Clear();//清空
richTextBox1.Clear();//清空
richTextBox2.Clear();//清空
richTextBox3.Clear();//清空
richTextBox4.Clear();//清空
currentDirectory = textBox1.Text;
//textBox2.Text = textBox1.Text;//无扩展性
textBox2.AppendText("The Path :"+currentDirectory);
SeachDirectory(currentDirectory);
}
}
/*方法*/
private void SeachDirectory(string path)
{
textBox3.AppendText("恭喜你:目录存在!");
string[] files = Directory.GetFiles(path);
string[] directorys = Directory.GetDirectories(path);
foreach (var i in files)
{
richTextBox1.AppendText(i + "\n");
}
foreach (var i in directorys)
{
richTextBox2.AppendText(i + "\n");
}
var exten_name =
from file in files
where Path.GetExtension(file) == currentextens
select file;
foreach (var i in exten_name)
{
richTextBox4.AppendText(Path.GetFileName(i) + "\n");
}
var exten_name2=
(from file in files
select Path.GetExtension(file)).Distinct();
foreach(var i in exten_name2)
{
richTextBox3.AppendText(i+"\n");
}
}
private void textBox1_TextChanged(object sender, EventArgs e) { /*即使是空也不可删*/}
private void textBox2_TextChanged(object sender, EventArgs e) {/*即使是空也不可删*/}
private void richTextBox1_TextChanged(object sender, EventArgs e) {/*即使是空也不可删*/}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
currentextens = ".pdf";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
currentextens = ".txt";
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
currentextens = ".rar";
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
currentextens = ".exe";
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
currentextens = ".jpg";
}
}
}