简单的文件查看Winform
来源:互联网 发布:java语言图形界面 编辑:程序博客网 时间:2024/06/06 00:39
根据文件或文件夹路劲查看相关信息
整体代码如下:
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; } }}
新手学习看看不错
阅读全文
0 0
- 简单的文件查看Winform
- 简单的文件查看编辑器
- 查看Linux大文件的简单命
- winform配置文件的简单使用
- winform配置文件的简单使用
- 简单的WinForm权限设计
- 简单的Winform启动画面
- winForm 做简单的绘图
- winForm 国际化的简单实现
- winForm 国际化的简单实现
- winform配置文件的简单使用
- winform配置文件的简单使用
- Winform简单的增删改
- winform配置文件的简单使用
- winform配置文件的简单使用
- 简单Tcp的winform窗体
- VS2008查看WinForm界面遇到的问题
- 一个简单的elf文件头查看工具
- 归一化互相关算法
- spring aop
- pycharm汉化包
- Spring Boot 的 WebSocket 开发说明文档
- com.android.volley.ParseError: cn.bmob.v3.exception.BmobException: responseHeaders does not containt
- 简单的文件查看Winform
- WPF Step By Step 系列-Prism框架在项目中使用
- 【JavaScript】省市联动示例
- 7.如何在Visio中写下角标和上角标
- 继承HorizontalScrollView的自定义轮播控件
- android极少人知道的冒号小技巧
- Android自定义时间控件选择开始时间到结束时间
- 将应用打包为 Snaps
- 输入校验一(js实现用户名校验)