简单的文件查看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;        }    }}

新手学习看看不错

原创粉丝点击