C#实现文件管理器—by henryqin
2009-04-04 08:22:43 来源:WEB开发网//上下文菜单项
ContextMenuStrip1.Items.Add(msEditNew);
ContextMenuStrip1.Items.Add(msEditBar1);
ContextMenuStrip1.Items.Add(msEditCut);
ContextMenuStrip1.Items.Add(msEditPaste);
ContextMenuStrip1.Items.Add(msEditDelete);
ContextMenuStrip1.Items.Add(msEditBar2);
这样就OK了,你在目标控件上按右键时将弹出快捷菜单。
通过上面的介绍,大家应该能明白本例中要实现的效果,需要用到的控件及重要属性等,下面,就可以切入正题,如何通过编程,实现我们上面提到的功能。呵呵,再提一点,也是比较重要的:C#是一种面向对象的编程语言,因此,在编程之前我们就要有“面向对象编程”的思想,在编程的过程中贯彻这样的思想,
同时,这也是符合目前公司里的编程思想。还有一点,尽量做到代码的规范,可以为以后再次开发或别人阅读我们程序起到简便明了的作用。
主要有两个类:Folder.cs和Txt.cs,分别是对文件和文本的相关操作。类里的方法,我都做了详尽的注释,如果还有什么不明白,可以联系我。
Folder.cs
CODE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileManager.Class
{
public class Folder
{
/// <summary>
/// 新建文件夹
/// </summary>
/// <param name="filename">文件夹名</param>
/// <param name="path">文件夹路径</param>
public static void NewFolder(string foldername, string path)
{
foldername.Trim();
//如果输入信息为空,提示
if (foldername == "")
{
MessageBox.Show("目录名不能为空");
return;
}
else
{
string FullName = path + "" + foldername;
//如果该文件以及存在
if (Directory.Exists(FullName))
{
MessageBox.Show("该目录已经存在,请重命名");
return;
}
else
{
//新建文件夹
Directory.CreateDirectory(FullName);
}
}
}
/// <summary>
/// 新建文件夹(Enter键触发)
/// </summary>
/// <param name="filename">文件夹名</param>
/// <param name="path">文件夹路径</param>
public static void Enter(string foldername, string path)
{
foldername.Trim();
if (foldername == "")
{
MessageBox.Show("目录名不能为空!");
return;
}
else if (Directory.Exists(path + "" + foldername))
{
MessageBox.Show("该目录以及存在,请重新命名");
return;
}
else
{
Directory.CreateDirectory(path + "" + foldername);
}
}
/// <summary>
/// //获取文件夹名,
截取“”
/// </summary>
/// <param name="DirectoryPath">文件夹完整路径</param>
/// <returns></returns>
internal static int DirectoryName(string DirectoryPath)
{
int j = 0;
char[] c = DirectoryPath.ToCharArray();
for (int i = c.Length - 1; i >= 0; i--)//从后面截取
{
j = i;
if (c[i].ToString() == "")
{
break;//遇""调处,并返回""的位置
}
}
return j + 1;
}
/// <summary>
/// 在指定目录下新建一个文件夹
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
public static void CreateDir(string path, string name)
{
Directory.CreateDirectory(path + "" + name);
}
/// <summary>
/// 复制文件
/// </summary>
/// <param name="oldDir">原文件所在文件夹路径</param>
/// <param name="Name">文件名</param>
/// <param name="newDir">新文件夹路径</param>
public static void CopyFile(string oldDir, string Name, string newDir)
{
File.Copy(oldDir + "" + Name, newDir + "" + Name);
}
/// <summary>
/// 复制文件
/// </summary>
/// <param name="oldDir">原文件所在文件夹路径</param>
/// <param name="newDir">新文件夹路径</param>
public static void CopyFile(string oldDir, string newDir)
{
string name = oldDir.Substring(DirectoryName(oldDir));
File.Copy(oldDir, newDir + "" + name);
}
/// <summary>
/// 删除指定文件夹
/// </summary>
/// <param name="strDir">文件夹完整路径</param>
/// <param name="recursive">是否删除文件夹子项</param>
public static void DeleteFolder(string strDir, bool recursive)
{
Directory.Delete(strDir, recursive);
}
/// <summary>
/// 返回指定路径的DirectoryInfo
/// </summary>
/// <returns></returns>
public static DirectoryInfo GetDir(string path)
{
return new DirectoryInfo(path);
}
}
}
Txt.cs
CODE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
namespace FileManager.Class
{
public class Txt
{
public Txt()
{
}
/// <summary>
/// 新建文件
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="path">文件路径</param>
public static void NewFile(string filename,string path)
{
filename.Trim();
if (filename == "")
{
MessageBox.Show("文件名不能为空~!");
}
else
{
if (File.Exists(path + "" + filename+".txt"))
{
MessageBox.Show("该文件名已经存在,请重命名");
}
else
{
string FullName = path + "" + filename + ".txt"; //获得文件完整信息
StreamWriter Sw = File.CreateText(FullName);
}
}
}
/// <summary>
/// 新建文件(Enter键触发)
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="path">文件路径</param>
public static void Enter(string filename, string path)
{
filename.Trim();
if (filename == "")
{
MessageBox.Show("文件名不能为空~!");
}
else
{
if (File.Exists(path + "" + filename+".txt"))
{
MessageBox.Show("该文件名已经存在,请重命名");
}
else
{
string FullName = path + "" + filename + ".txt"; //获得文件完整信息
StreamWriter Sw = File.CreateText(FullName);
}
}
}
/// <summary>
/// 读取显示
/// </summary>
/// <param name="filename">文本文件名</param>
/// <param name="contents">内容</param>
public static string Display(string filename)
{//读取显示
try
{
string contents;
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //读出所打开的文本文件
StreamReader reader = new StreamReader(fs); //实例化一个streamreader
contents = reader.ReadToEnd(); //将数据显示
fs.Close();
return contents;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}
/// <summary>
/// 打开
/// </summary>
/// <param name="ofd">打开对话框</param>
/// <param name="fullname">文本文件名</param>
/// <param name="contents">内容</param>
public static void Open(OpenFileDialog ofd,string fullname,string contents)
{//打开文本
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileName = ofd.FileName; //文件名
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
fullname = fileName;
contents = reader.ReadToEnd();
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="fullname">文本文件名</param>
/// <param name="contents">内容</param>
public static void Save(string fullname,string contents)
{//保存
try
{
StreamWriter writer = new StreamWriter(fullname);
writer.Write(contents); //用write()方法把txtContent的数据写入文件
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="sfd">保存对话框</param>
/// <param name="contents">内容</param>
public static void SaveAs(SaveFileDialog sfd,string contents)
{
if (sfd.ShowDialog() == DialogResult.OK)
{
string fileName = sfd.FileName;
try
{
Stream stream = File.OpenWrite(fileName);
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(contents);
writer.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// 粗体
/// </summary>
/// <param name="rtb"></param>
public static void Bold(RichTextBox rtb)
{
Font newFont = new Font(rtb.SelectionFont,
(rtb.SelectionFont.Bold ?
rtb.SelectionFont.Style & ~FontStyle.Bold :
rtb.SelectionFont.Style | FontStyle.Bold));
rtb.SelectionFont = newFont;
}
/// <summary>
/// 斜体
/// </summary>
/// <param name="rtb"></param>
public static void Italic(RichTextBox rtb)
{
Font newFont = new Font(rtb.SelectionFont,
(rtb.SelectionFont.Italic ?
rtb.SelectionFont.Style & ~FontStyle.Italic :
rtb.SelectionFont.Style | FontStyle.Italic));
rtb.SelectionFont = newFont;
}
/// <summary>
/// 下划线
/// </summary>
/// <param name="rtb"></param>
public static void Underline(RichTextBox rtb)
{
Font newFont = new Font(rtb.SelectionFont,
(rtb.SelectionFont.Underline ?
rtb.SelectionFont.Style & ~FontStyle.Underline :
rtb.SelectionFont.Style | FontStyle.Underline));
rtb.SelectionFont = newFont;
}
/// <summary>
/// 复制
/// </summary>
/// <param name="rtb"></param>
public static void Copy(RichTextBox rtb)
{
if (rtb.SelectedText.Equals(""))
return;
Clipboard.SetDataObject(rtb.SelectedText, true);
}
/// <summary>
/// 剪切
/// </summary>
/// <param name="rtb"></param>
public static void Cut(RichTextBox rtb)
{
if (rtb.SelectedText.Length > 0)
{
try
{
rtb.Cut();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// 粘贴
/// </summary>
/// <param name="rtb"></param>
public static void Paste(RichTextBox rtb)
{
rtb.Paste();
}
/// <summary>
/// 退出
/// </summary>
/// <param name="fullname">文本文件全名</param>
/// <param name="contents">内容</param>
public static void Exit(string fullname,string contents)
{
if (MessageBox.Show(" 是否保存文件",
"提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
StreamWriter writer = new StreamWriter(fullname);
writer.Write(contents); //用write()方法把txtContent的数据写入文件
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// 在指定目录新建一个文本文件
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
public static StreamWriter CreateText(string path, string name)
{
return File.CreateText(path + "" + name);
}
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="strFile">文件完整路径</param>
public static void DeleteFile(string strFile)
{
File.Delete(strFile);
}
}
}
更多精彩
赞助商链接