简单的文件查看Winform
2017-11-30 17:10
218 查看
根据文件或文件夹路劲查看相关信息
整体代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { //保存当前访问路径 private string currentFolderPath; public Form1() { InitializeComponent(); } private void buttonDelete_Click(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath,txtFileName.Text); string query = "Really delete the file?\n" + filePath; if (MessageBox.Show(query,"Delete file?",MessageBoxButtons.YesNo)==DialogResult.Yes) { File.Delete(filePath); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to delete file. The following exception" +" occurred :\n"+ex.Message," Failed"); } } private void buttonDisplay_Click(object sender, EventArgs e) { try { string folderPath = txtBoxInput.Text; DirectoryInfo theFolder = new DirectoryInfo(folderPath); if (theFolder.Exists) { DisplayFolderList(theFolder.FullName); return; } FileInfo theFile = new FileInfo(folderPath); if (theFile.Exists) { DisplayFolderList(theFile.Directory.FullName); int index = listBoxFiles.Items.IndexOf(theFile.Name); listBoxFiles.SetSelected(index, true); return; } throw new FileNotFoundException("There is no file or folder with " + "this name: " + txtBoxInput.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void buttonUp_Click(object sender, EventArgs e) { try { string folderPath = new FileInfo(currentFolderPath).DirectoryName; DisplayFolderList(folderPath); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void listBoxFiles_SelectedIndexChanged(object sender, EventArgs e) { try { if (listBoxFiles.SelectedItems.Count>0) { string selectedString = listBoxFiles.SelectedItem.ToString(); string fullFileName = Path.Combine(currentFolderPath, selectedString); DisplayFileInfo(fullFileName); } else { return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void listBoxFolders_SelectedIndexChanged(object sender, EventArgs e) { try { if (listBoxFiles.SelectedItems.Count > 0) { string selectedString = listBoxFolders.SelectedItem.ToString(); string fullPathName = Path.Combine(currentFolderPath, selectedString); DisplayFolderList(fullPathName); } else { return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //根据文件夹路径加载到listbox protected void DisplayFolderList(string folderFullName) { DirectoryInfo theFolder = new DirectoryInfo(folderFullName); if (!theFolder.Exists) { throw new DirectoryNotFoundException("Folder not found: " + folderFullName); } ClearAllFiled(); DisableMoveFeatures(); txtBoxFolder.Text = theFolder.FullName; currentFolderPath = theFolder.FullName; foreach (DirectoryInfo nextFolder in theFolder.GetDirectories()) { listBoxFolders.Items.Add(nextFolder.Name); } foreach (FileInfo nextFile in theFolder.GetFiles()) { listBoxFiles.Items.Add(nextFile.Name); } } void DisableMoveFeatures() { textBoxNewPath.Text = ""; textBoxNewPath.Enabled = false; buttonCopyTo.Enabled = false; buttonMoveTo.Enabled = false; buttonDelete.Enabled = false; } protected void ClearAllFiled() { listBoxFiles.Items.Clear(); listBoxFolders.Items.Clear(); txtBoxFolder.Text = ""; txtFileName.Text = ""; txtBoxFolder.Text = ""; txtBoxCreationTime.Text = ""; txtBoxFileSize.Text = ""; txtBoxLastAccessTime.Text = ""; txtBoxLastWriteTime.Text = ""; textBoxNewPath.Text = ""; } private void buttonMoveTo_Click(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath,txtFileName.Text); string query = "Really move the file\n" + filePath + "\nto" + textBoxNewPath.Text + "?"; if (MessageBox.Show(query,"Move File?",MessageBoxButtons.YesNo)==DialogResult.Yes) { File.Move(filePath,textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to move file.The following exception" +" occurred:\n"+ex.Message,"Failed"); } } private void buttonCopyTo_Click(object sender, EventArgs e) { try { string filePath = Path.Combine(currentFolderPath, txtFileName.Text); string query = "Really copy the file\n" + filePath + "\nto" + textBoxNewPath.Text + "?"; if (MessageBox.Show(query,"Copy File?",MessageBoxButtons.YesNo)==DialogResult.Yes) { File.Copy(filePath,textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch (Exception ex) { MessageBox.Show("Unable to copy file. The following exception" +" occurred:\n"+ex.Message,"Failed"); } } protected void DisplayFileInfo(string fileFullName) { FileInfo theFile = new FileInfo(fileFullName); if (!theFile.Exists) { throw new FileNotFoundException("File not found: "+fileFullName); } txtFileName.Text = theFile.Name; txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString(); txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongTimeString(); txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongTimeString(); txtBoxFileSize.Text = theFile.Length.ToString()+ " bytes"; //enable move,copy delete buttons textBoxNewPath.Text = theFile.FullName; textBoxNewPath.Enabled = true; buttonCopyTo.Enabled = true; buttonDelete.Enabled = true; buttonMoveTo.Enabled = true; } } }
新手学习看看不错
相关文章推荐
- C#Winform基础 treeview 简单文件夹管理器 查看文件夹下的文件,子文件下的文件
- WinForm实现简单的拖拽文件到出题的功能(C#)(3)
- winform配置文件的简单使用
- linux查看文件系统块大小与内存页大小的简单方法
- 论坛源码推荐(9月24日):Path 4.0菜单按钮,简单查看和阅读PDF文件
- Ubuntu下查看二进制文件的简单方法
- winform:简单文件资源管理器
- 使用TreeView和listView控件查看本机磁盘文件夹及文件(WinForm)
- 使用TreeView和listView控件查看本机磁盘文件夹及文件(WinForm)
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 一个简单的elf文件头查看工具
- 在txt文件里进行查询(winform案例简单)
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- winform 实现选择文件和选择文件夹对话框的简单实例
- Liunx查看文件:Less命令简单介绍
- 使用BBED查看数据文件头(block# 1)的简单使用及查询DBID/DB_NAME等信息
- 使用TreeView和listView控件查看本机磁盘文件夹及文件(WinForm)
- 用C#Winform写个简单的批量清空文件内容和删除文件的小工具
- winform配置文件的简单使用
- AndroidStudio快速查看签名文件MD5、SHA1值的一个超简单方法。