File类和Directory类分别用来对文件和各种目录进行操作,这两类可以被实例化,但不能被其他类集成。

1. File类(静态类)

File类支持对文件的基本操作,它包括用于创建、复制、删除、移动和打开文件的静态方法,并协助创建FileStream对象。

2. Directory类(静态类)

Directory类公开了用于创建、移动、枚举、删除目录和子目录的静态方法。

举例1:文件的创建

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace Test01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty) //判断输入的文件名是否为空
{
MessageBox.Show("文件名不能为空!");
}
else
{
if (File.Exists(textBox1.Text)) //使用File类的Exists方法判断要创建的文件是否存在
{
MessageBox.Show("该文件已经存在");
}
else
{
File.Create(textBox1.Text); //使用File类的Create方法创建文件
}
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}

举例2:文件夹的创建

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; namespace Test02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty) //判断输入的文件夹名称是否为空
{
MessageBox.Show("文件夹名称不能为空!");
}
else
{
if (Directory.Exists(textBox1.Text)) //使用Directory类的Exists方法判断要创建的文件夹是否存在
{
MessageBox.Show("该文件夹已经存在");
}
else
{
Directory.CreateDirectory(textBox1.Text); //使用Directory类的CreateDirectory方法创建文件夹
}
}
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}

File类和Directory类的更多相关文章

  1. File类、FileInfo类、Directory类、DirectoryInfo类

    File类.Directory类,都是静态类,可以直接使用类名 FileInfo类.DirectoryInfo类,都是动态类,需要new对象,通过对象来操作 [文件的创建.复制.移动.删除]using ...

  2. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  3. 文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类

    文件及文件夹操作: C/S:WinForm可以操作客户端文件 Client ServerB/S:Brower Server 命名空间:using system .IO; 1. File类: 创建:Fi ...

  4. C#文件操作常用相关类(Directory类、File类、Path类)

    1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...

  5. Path类与Directory类与File类

    阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo  对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...

  6. C# IO操作(二)File类和Directory类的常用方法

    本篇主要介绍一些常用的IO操作,对文件和目录的操作:留给自己复习之用. 1.创建文件 string sPath1=Path.GetDirectoryName(Assembly.GetExecuting ...

  7. path类和directory类对文件的路径或目录进行操作

    Path: 对文件或目录的路径进行操作(很方便)[只是对字符串的操作] 1.目录和文件操作的命名控件System.IO    2.string Path.ChangeExtension(string ...

  8. FileInfo类和DirectoryInfo类

    FileInfo类和DirectoryInfo类可以方便地对文件和文件夹进行操作. 1. FileInfo类(非静态类) FileInfo类和File类之间许多方法调用都是相同的,但是FileInfo ...

  9. C# File和Directory类

    File和Directory类 作为实用类,File和Directory类都提供了许多方法,用于处理文件系统以及其中的文件和目录.这些是静态方法,涉及移动文件.查询和更新属性并创建FileStream ...

随机推荐

  1. BZOJ4130:[PA2011]Kangaroos

    浅谈\(K-D\ Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...

  2. Spark Shuffle大揭秘

    什么是Shuffle: Shuffle中文翻译为“洗牌”,需要Shuffle的关键原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算. Shuffle面临的问题: 1. 数据量非常大: ...

  3. 运行flask程序

    Command Line Interface Installing Flask installs the flask script, a Click command line interface, i ...

  4. MyBatis定制SQL集中特殊的处理方式

    举例说明: 1.查询 姓为林的数据  LIKE 3种 Select * from sys_user where user_name like '林%' 关于结果集合多个参数传递数据 特殊字符的处理

  5. C语言第二次实验作业

    PTA ================= 11-6 方阵循环右移 --------------- 本题要求编写程序,将给定n×n方阵中的每个元素循环向右移m个位置,即将第0.1.....n-1列变换 ...

  6. HTML5两个打包工具

    AppCan:http://www.appcan.cn/ HBulider:http://www.dcloud.io/

  7. expected declaration specifiers or '...' before string constant

    /work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...

  8. IRedisClient

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  9. sharepoint文档库中日期显示详细日期,不显示几天前

    文档库---库设置----栏

  10. HDU 6162 树链剖分

    题意:给你一颗树,每个节点有有一个权值,每次询问从x到y的最短路上权值在c到d之间的所有的点的权值和是多少. 思路:肯定要用树剖,因为询问c到d之间这种操作树上倍增很难做,但是用其它数据结构可以比较好 ...