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();
}
}
}
}