weixin_45653310
唯祯
2020-05-08 19:23
采纳率: 100%
浏览 194

C# 如何通过文件名来搜索bin目录下的txt文件,并按列导入到listview中?

C# 如何通过文件名来搜索bin目录下的txt文件,并按列导入到listview中?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳

    完整的代码:https://download.csdn.net/download/caozhy/12401317

    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.Reflection;
    using System.IO;
    
    namespace Q1069478
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                listView1.Items.Clear();
                var files = Directory.GetFiles(textBox1.Text, "*.txt", SearchOption.AllDirectories);
                foreach (var f in files)
                {
                    listView1.Items.Add(Path.GetFileName(f));
                }
            }
        }
    }
    
    
    namespace Q1069478
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.listView1 = new System.Windows.Forms.ListView();
                this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
                this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem;
                this.textBox1.Location = new System.Drawing.Point(26, 16);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(323, 20);
                this.textBox1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(355, 14);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(87, 22);
                this.button1.TabIndex = 1;
                this.button1.Text = "List Text file";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // listView1
                // 
                this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                this.columnHeader1});
                this.listView1.GridLines = true;
                this.listView1.Location = new System.Drawing.Point(26, 42);
                this.listView1.Name = "listView1";
                this.listView1.Size = new System.Drawing.Size(416, 335);
                this.listView1.TabIndex = 2;
                this.listView1.UseCompatibleStateImageBehavior = false;
                this.listView1.View = System.Windows.Forms.View.Details;
                // 
                // columnHeader1
                // 
                this.columnHeader1.Text = "FileName";
                this.columnHeader1.Width = 300;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(468, 398);
                this.Controls.Add(this.listView1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.ListView listView1;
            private System.Windows.Forms.ColumnHeader columnHeader1;
        }
    }
    
    

    图片说明

    点赞 评论

相关推荐