zoukankan      html  css  js  c++  java
  • 第一个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";
    }
    }
    }



  • 相关阅读:
    数据结构开发(23):二叉树中结点的查找、插入、删除与清除操作
    数据结构开发(22):二叉树的转换、深层特性与存储结构设计
    数据结构开发(21):树中属性操作与层次遍历
    数据结构开发(20):树中结点的查找、插入、清除与删除操作
    数据结构开发(19):树的定义、操作、存储结构与实现
    数据结构开发(18):归并排序和快速排序
    数据结构开发(17):冒泡排序和希尔排序
    HTTP协议的内容协商
    长轮询实现消息推送
    XHR的应用场景
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2300589.html
Copyright © 2011-2022 走看看