1. // Attempt to run the file.
  2. System.Diagnostics.Process.Start(filename);
  1. //folderCol 可以存放一个路径的 栈(用于返回功能的设计)
  2. private System.Collections.Specialized.StringCollection folderCol;
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace ListView
  12. {
  13. public partial class Form1 : Form
  14. {
  15. private System.Collections.Specialized.StringCollection folderCol;
  16.  
  17. public Form1()
  18. {
  19. InitializeComponent();
  20.  
  21. // Init ListView and folder collection
  22. folderCol = new System.Collections.Specialized.StringCollection();
  23. CreateHeadersAndFillListView();
  24. PaintListView(@"C:\");
  25. folderCol.Add(@"C:\");
  26.  
  27. }
  28.  
  29. private void CreateHeadersAndFillListView()
  30. {
  31. ColumnHeader colHead;
  32.  
  33. // First header
  34. colHead = new ColumnHeader();
  35. colHead.Text = "Filename";
  36. listViewFilesAndFolders.Columns.Add(colHead); // Insert the header
  37.  
  38. // Second header
  39. colHead = new ColumnHeader();
  40. colHead.Text = "Size";
  41. listViewFilesAndFolders.Columns.Add(colHead); // Insert the header
  42.  
  43. // Third header
  44. colHead = new ColumnHeader();
  45. colHead.Text = "Last accessed";
  46. listViewFilesAndFolders.Columns.Add(colHead); // Insert the header
  47. }
  48.  
  49. private void PaintListView(string root)
  50. {
  51. try
  52. {
  53. // Two local variables that are used to create the items to insert
  54. ListViewItem lvi;
  55. ListViewItem.ListViewSubItem lvsi;
  56.  
  57. // If there’s no root folder, we can’t insert anything.
  58. if (string.IsNullOrEmpty(root))
  59. return;
  60.  
  61. // Get information about the root folder.
  62. DirectoryInfo dir = new DirectoryInfo(root);
  63.  
  64. // Retrieve the files and folders from the root folder.
  65. DirectoryInfo[] dirs = dir.GetDirectories(); // Folders
  66. FileInfo[] files = dir.GetFiles(); // Files
  67.  
  68. // Clear the ListView. Note that we call the Clear method on the
  69. // Items collection rather than on the ListView itself.
  70. // The Clear method of the ListView remove everything, including column
  71. // headers, and we only want to remove the items from the view.
  72. listViewFilesAndFolders.Items.Clear();
  73.  
  74. // Set the label with the current path.
  75. labelCurrentPath.Text = root;
  76.  
  77. // Lock the ListView for updates.
  78. listViewFilesAndFolders.BeginUpdate();
  79.  
  80. // Loop through all folders in the root folder and insert them.
  81. foreach (DirectoryInfo di in dirs)
  82. {
  83. // Create the main ListViewItem.
  84. lvi = new ListViewItem();
  85. lvi.Text = di.Name; // Folder name
  86. lvi.ImageIndex = ; // The folder icon has index 0
  87. lvi.Tag = di.FullName; // Set the tag to the qualified path of the
  88. // folder
  89.  
  90. // Create the two ListViewSubItems.
  91. lvsi = new ListViewItem.ListViewSubItem();
  92. lvsi.Text = ""; // Size—a folder has no size and so this column
  93. // is empty
  94. lvi.SubItems.Add(lvsi); // Add the subitem to the ListViewItem
  95.  
  96. lvsi = new ListViewItem.ListViewSubItem();
  97. lvsi.Text = di.LastAccessTime.ToString(); // Last accessed column
  98. lvi.SubItems.Add(lvsi); // Add the subitem to the ListViewItem.
  99.  
  100. // Add the ListViewItem to the Items collection of the ListView.
  101. listViewFilesAndFolders.Items.Add(lvi);
  102. }
  103.  
  104. // Loop through all the files in the root folder.
  105. foreach (FileInfo fi in files)
  106. {
  107. // Create the main ListViewItem.
  108. lvi = new ListViewItem();
  109. lvi.Text = fi.Name; // Filename
  110. lvi.ImageIndex = ; // The icon we use to represent a folder has
  111. // index 1.
  112. lvi.Tag = fi.FullName; // Set the tag to the qualified path of the
  113. // file.
  114.  
  115. // Create the two subitems.
  116. lvsi = new ListViewItem.ListViewSubItem();
  117. lvsi.Text = fi.Length.ToString(); // Length of the file
  118. lvi.SubItems.Add(lvsi); // Add to the SubItems collection
  119.  
  120. lvsi = new ListViewItem.ListViewSubItem();
  121. lvsi.Text = fi.LastAccessTime.ToString(); // Last Accessed Column
  122. lvi.SubItems.Add(lvsi); // Add to the SubItems collection
  123.  
  124. // Add the item to the Items collection of the ListView.
  125. listViewFilesAndFolders.Items.Add(lvi);
  126. }
  127.  
  128. // Unlock the ListView. The items that have been inserted will now
  129. // be displayed.
  130. listViewFilesAndFolders.EndUpdate();
  131. }
  132. catch (System.Exception err)
  133. {
  134. MessageBox.Show("Error: " + err.Message);
  135. }
  136. }
  137.  
  138. private void listViewFilesAndFolders_ItemActivate(object sender, EventArgs e)
  139. {
  140. // Cast the sender to a ListView and get the tag of the first selected
  141. // item.
  142. System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
  143. string filename = lw.SelectedItems[].Tag.ToString();
  144.  
  145. if (lw.SelectedItems[].ImageIndex != )
  146. {
  147. try
  148. {
  149. // Attempt to run the file.
  150. System.Diagnostics.Process.Start(filename);
  151. }
  152. catch
  153. {
  154. // If the attempt fails we simply exit the method.
  155. return;
  156. }
  157. }
  158. else
  159. {
  160. // Insert the items.
  161. PaintListView(filename);
  162. folderCol.Add(filename);
  163. }
  164. }
  165.  
  166. private void buttonBack_Click(object sender, EventArgs e)
  167. {
  168. if (folderCol.Count > )
  169. {
  170. PaintListView(folderCol[folderCol.Count - ].ToString());
  171. folderCol.RemoveAt(folderCol.Count - );
  172. }
  173. else
  174. PaintListView(folderCol[].ToString());
  175. }
  176.  
  177. private void radioButtonLargeIcon_CheckedChanged(object sender, EventArgs e)
  178. {
  179. RadioButton rdb = (RadioButton)sender;
  180. if (rdb.Checked)
  181. this.listViewFilesAndFolders.View = View.LargeIcon;
  182. }
  183.  
  184. private void radioButtonSmallIcon_CheckedChanged(object sender, EventArgs e)
  185. {
  186. RadioButton rdb = (RadioButton)sender;
  187. if (rdb.Checked)
  188. this.listViewFilesAndFolders.View = View.SmallIcon;
  189. }
  190.  
  191. private void radioButtonList_CheckedChanged(object sender, EventArgs e)
  192. {
  193. RadioButton rdb = (RadioButton)sender;
  194. if (rdb.Checked)
  195. this.listViewFilesAndFolders.View = View.List;
  196. }
  197.  
  198. private void radioButtonDetails_CheckedChanged(object sender, EventArgs e)
  199. {
  200. RadioButton rdb = (RadioButton)sender;
  201. if (rdb.Checked)
  202. this.listViewFilesAndFolders.View = View.Details;
  203. }
  204.  
  205. private void radioButtonTile_CheckedChanged(object sender, EventArgs e)
  206. {
  207. RadioButton rdb = (RadioButton)sender;
  208. if (rdb.Checked)
  209. this.listViewFilesAndFolders.View = View.Tile;
  210. }
  211. }
  212. }

c# ListView的更多相关文章

  1. 张高兴的 UWP 开发笔记:横向 ListView

    ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...

  2. Android—万能ListView适配器

    ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...

  3. Android—ListView条目背景为图片时,条目间距问题解决

    ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:

  4. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  5. listview下拉刷新和上拉加载更多的多种实现方案

    listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局        android系统为listview提供了addfootview ...

  6. Android listview和gridview以及view的区别

    GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...

  7. mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context

    需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...

  8. 【腾讯Bugly干货分享】跨平台 ListView 性能优化

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...

  9. android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)

    第三节(2):常用控件之ViewPager.日期时间相关.ListView  一.ViewPager 实例:结合PagerAdapter滑动切换图片  二.日期时间相关:AnalogClock\Dig ...

  10. 父ListView嵌套子ListView时点击事件没有响应

    转发请备注出处:http://www.cnblogs.com/LT5505/p/5972999.html 问题: 在ListView中嵌套ListView之后,子ListView会把父ListView ...

随机推荐

  1. javaScript表单焦点自动切换

    ---恢复内容开始--- <html> <head> <script> window.onload=function(){ var form=document.ge ...

  2. Memcache入门知识

    Memcache适合做缓存,是一款管理内存的很小的软件,实现对内存数据的管理,一般我们用memcache存储临时数据,因为内存不能储存永久化的数据,内存里面的数据,断电就消失了. memcache可以 ...

  3. 我的开源框架之Accordion控件

    需求: (1)实现手风琴面板控件,支持静态HTML与JSON方式创建控件 (2)支持远程加载数据 (3)支持面板激活.远程加载事件注册 (4)支持动态添加.删除项目 实现图例 客户代码 <div ...

  4. HTML5 canvas 在线画笔绘图工具(二)

    Canvas+Javascript 带图标的工具条制作 TToolbar 工具条是由一个TToolbar对象和两个按钮对象(TImageButton.TColorButton)组成,因为之前我大部分时 ...

  5. mac terminal的使用技巧

    1. 多tab支持    1)terminal y也是支持多tab的, Cmd+T可以打开一个新的tab    2) cmd + shift + { / } 可以在tab间切换   2. termia ...

  6. Python学习笔记三,数组list和tuple

    list也就是列表的意思,可以存储一组数据集合,比如classmates=['zhangsan','lisi','123']每个数据用单引号包裹,逗号隔开.

  7. HTTP之I/O模型图MPM详细解析

    高度模块化:DSO MPM:多路处理模块      prefork-->一个主进程+多个工作进程,每个工作进程处理多个请求      worker-->一个主进程+多个工作进程,每个工作进 ...

  8. php word转HTML

    因为安装的的xampp不知道如何查看我的Apache版本是多少,就先把com.allow_dcom=true打开了,但是仍旧报错说找不到com类,然后就把下面的extension扩展添加到php.in ...

  9. opencv中的图像区域复制

    openCV作为已经成熟的开源库,很多操作它都已经有了高效,使用方便的方法.我的应用场景是这样的,从一张大图片中抠出一小部分,然后处理这一小部分后再放到大图像中.对于抠出来可以这样实现: Rect r ...

  10. Asp.net Web.Config - 配置元素customErrors

    Asp.net配置文件的配置方式,其实在MSDN里面是写得最清楚的了.可惜之前一直未曾了解到MSDN的强大. 先贴个地址:http://msdn.microsoft.com/zh-cn/library ...