C# LINQ基本用法(2) ---LINQ 数组

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;
/*---------------------------------------------
 * 标题:LINQ to Objects 基本用法;
 * 作者:靳志远(jack15850798154)
 * 时间:2010-03-19
 * 地点:江苏南京
 * 说明 :通过 LINQ来实现操作 数组的 功能。
 *----------------------------------------------
 */
namespace LINQ_ShuZhu
{

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      /// <summary>
      /// 通过 LINQ实现把 获取文件夹下 的所有文件放入 到ListView中。
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.Items.Clear();
                this.textBox1.Text = folderBrowserDialog1.SelectedPath;
                List<FileInfo> myfiles = new List<FileInfo>();
                foreach (string strfile in Directory.GetFiles(this.textBox1.Text))
                {
                    myfiles.Add(new FileInfo(strfile));
                }
                var values = from strFile in myfiles
                             group strFile by strFile.Extension into FExten
                             orderby FExten.Key
                             select FExten;
                foreach (var vFiles in values)
                {
                    foreach (var f in vFiles)
                    {
                        listView1.Items.Add(f.FullName);
                    }
                }

            }
        }
     /// <summary>
     /// 通过ListView1事件 把数据放入到TextBox文本框中。
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
        private void listView1_Click(object sender, EventArgs e)
        {
            FileInfo myFile = new FileInfo(listView1.SelectedItems[0].Text);
            string[] strAttribute = new string[]{myFile.Name,Convert.ToDouble(myFile.Length/1024).ToString()
            ,myFile.Extension,myFile.CreationTime.ToString(),myFile.IsReadOnly.ToString(),myFile.LastWriteTime.ToString()
           
            };
            var values = from str in strAttribute
                         select new
                         {
                             Name = strAttribute[0].ToString(),
                             Size = strAttribute[1].ToString(),
                             Exten = strAttribute[2].ToString(),
                             CTime = strAttribute[3].ToString(),
                             ReadOnly = strAttribute[4].ToString(),
                             WTime = strAttribute[5].ToString(),
                         };
            foreach (var v in values)
            {
                this.textBox2.Text = v.Name.ToString();
                this.textBox3.Text = v.Exten.ToString();
                this.textBox4.Text = v.Size.ToString();
                this.textBox5.Text = v.CTime.ToString();
                this.textBox6.Text = v.ReadOnly.ToString();
                this.textBox7.Text =v.WTime.ToString();
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值