C#面向对象程序设计(8)

内容纲要

8.1 文件对话框 

8.1.1 OpenFileDialog 对话框 

在 C# 中共有 3 种文件对话框,分别实现不同的功能 

  • 打开文件的对话框 OpenFileDialog、
  • 保存文件的对话框SaveFileDialog 
  • 打开文件夹的对话框 FolderBroswerDialog 

OpenFileDialog(打开文件对话框)

示例:

private void btnOpen_Click(object sender, EventArgs e)
{
        if (ofDlg.ShowDialog() == DialogResult.OK)//判断是否点击的为“打开”按钮
        {
        	txtPath.Text = ofDlg.FileName;
        }
}

OpenFileDialog 常用属性见表 

  • InitialDirectory :对话框的初始目录 
  • Filter :文件筛选器,按“显示名称 | 类型”格式编写 
  • FilterIndex  :在对话框中选择的文件筛选器的索引 
  • Title :将显示在对话框标题栏中的字符 
  • AddExtension:是否自动添加默认扩展名
  • CheckFileExists :在用户指定不存在的文件时是否显示警告 
  • CheckPathExists:在对话框返回之前,检查指定路径是否存在 
  • DefaultExt:默认扩展名

8.1.2 SaveFileDialog 对话框 

SaveFileDialog(保存文件对话框)常用于软件中的“另存为”功能 

示例:

private void btnSave_Click(object sender, EventArgs e)
{
        if (sfDlg.ShowDialog() == DialogResult.OK)//判断是否点击的为“保存”按钮
        {
	txtPath.Text = sfDlg.FileName;
        }
}

8.1.3 FolderBroswerDialog 对话框 

FolderBroswerDialog(文件夹浏览对话框)的功能是选择指定文件夹 

FolderBroswerDialog 常用属性见表 

  • Description :显示在对话框视图上方的字符串,用来指定显示给用户的指导信息 
  • RootFolder :设置根文件夹位置 
  • SelectedPath:对话框中最先选择的文件夹或用户,最后选择的文件夹完整路径 
  • ShowNewFolderButton :对话框中是否包括“新建文件夹”按钮 

FolderBroswerDialog 常用方法为 ShowDialog() 

示例:

private void btnBrowse_Click(object sender, EventArgs e)
{
        if (fbDlg.ShowDialog() == DialogResult.OK)//判断是否点击的为“浏览”按钮
        {
	txtPath.Text = fbDlg.SelectedPath;
        }
}

8.2 文件常用操作 

8.2.2 文件常用操作方法 

在 C# 中如果对文件进行创建、复制和删除等少量操作时,一般使用 File 类,常用方法见表 

  • Exists():用于检查指定文件是否存在 
  • Copy() :将现有文件复制到新文件中 
  • Move():将指定文件移动到新位置 
  • Delete() :删除指定文件

文件的复制与删除 

示例:

static void Main(string[] args)
{ 
        string path = @"e:\Test.txt";//去除字符串中的转义字符含义
        if (File.Exists(path))
        {
	File.Copy(path,@"f:\Test2.txt"); 
	File.Delete(path); 
        }
}

文件的移动 

public partial class FileMove : Form
{
//获取文件路径按钮方法
         private void btnSearchPath_Click(object sender, EventArgs e)
         {
	if (ofDlg.ShowDialog()==DialogResult.OK)
	{
		txtOldPath.Text = ofDlg.FileName;
	}
         }
}  

public partial class FileMove : Form
{
         private void btnSavePath_Click(object sender, EventArgs e)
         {
	sfDlg.AddExtension = true;
	sfDlg.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
	sfDlg.FilterIndex = 1;
	if (sfDlg.ShowDialog() == DialogResult.OK)
	{
		txtNewPath.Text = sfDlg.FileName;
	}
         }
}
//确认按钮方法
public partial class FileMove : Form
{
         private void btnConfirmed_Click(object sender, EventArgs e)
         {
	string path = txtOldPath.Text;
	if (File.Exists(path))
	{
		File.Move(path, txtNewPath.Text);
		MessageBox.Show(" 移动成功。");
	}
         }
}

8.3 文件夹常用操作 

8.3.1 文件夹操作方法介绍 

同文件操作一样,在 C# 中对文件夹进行创建、移动和删除等操作有 Directory 类和 DirectoryInfo 类,常用方法见表 

  • Exists():用于检查指定文件夹是否存在
  • CreateDirectory():在指定路径创建所有目录和子目录
  • Move():将指定文件夹移动到新位置 
  • Delete(string):删除指定空文件夹 
  • Delete(string, bool):删除指定的文件夹并删除该文件夹中的所有子文件夹和文件 

8.3.2 文件夹常用操作方法 

文件夹的相关操作 

示例:

static void Main(string[] args)
{ 
      string path = @"E:\Test";
      if (Directory.Exists(path))
      {
	Directory.Delete(path, true);//文件夹的删除操作
      }
}


public partial class FileMove : Form
{
//选择文件夹按钮方法
        private void btnSelectDirectory_Click(object sender, EventArgs e)
        {
	FolderBrowserDialog fbDlg = new FolderBrowserDialog();
	if (fbDlg.ShowDialog() == DialogResult.OK)
	{
		txtDirectoryPath.Text = fbDlg.SelectedPath;
		DirectoryPath = txtDirectoryPath.Text;
	}
        }
}  


private void btnCreateDirectory_Click(object sender, EventArgs e)
{
//创建文件夹按钮方法
        if (!String.IsNullOrEmpty(DirectoryPath))
        {
	string Path = DirectoryPath + "\\" + txtDirectoryName.Text;
	Directory.CreateDirectory(Path);
	MessageBox.Show(" 创建成功。");
        }
        else
        {
	MessageBox.Show(" 请选择需要创建目录的文件夹。");
        }
}


private void btnMoveDirectory_Click(object sender, EventArgs e)
{
        //其他代码省略…
        if (!String.IsNullOrEmpty(txtMoveNewPath.Text))
        {
//移动文件夹按钮方法
	string NemPath = fbDlg.SelectedPath + "\\" + txtMoveNewPath.Text;
	Directory.Move(DirectoryPath, NemPath);
	MessageBox.Show(" 移动成功。");
        }
}
//删除文件夹按钮方法
private void btnDeleteDirectory_Click(object sender, EventArgs e)
{
        if (Directory.Exists(DirectoryPath))
        {
	Directory.Delete(DirectoryPath, true);
	MessageBox.Show(" 删除成功。");
        }
        else
        {
	MessageBox.Show(" 请选择正确的路径。");
        }
}

总结 :

  1. File 类中常用的方法有: Exists() 方法、Copy() 方法、Move() 方法以及 Delete() 方法 
  2. Directory 类中常用的方法有:Exists() 方法、CreateDirectory() 方法、Move() 方法、Delete(string) 方法以及Delete(string, bool) 方法 

 

给TA打赏
共{{data.count}}人
人已打赏
C#

C#面向对象程序设计(7)

2022-8-16 16:40:51

C#

C#面向对象程序设计(9)

2022-8-17 10:44:08

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索