您的位置:首页 > 编程语言 > C#

简单的文件查看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 文件管理器
相关文章推荐