File类和Directory类
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类的更多相关文章
- File类、FileInfo类、Directory类、DirectoryInfo类
File类.Directory类,都是静态类,可以直接使用类名 FileInfo类.DirectoryInfo类,都是动态类,需要new对象,通过对象来操作 [文件的创建.复制.移动.删除]using ...
- .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化
1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...
- 文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类
文件及文件夹操作: C/S:WinForm可以操作客户端文件 Client ServerB/S:Brower Server 命名空间:using system .IO; 1. File类: 创建:Fi ...
- C#文件操作常用相关类(Directory类、File类、Path类)
1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...
- Path类与Directory类与File类
阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo 对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...
- C# IO操作(二)File类和Directory类的常用方法
本篇主要介绍一些常用的IO操作,对文件和目录的操作:留给自己复习之用. 1.创建文件 string sPath1=Path.GetDirectoryName(Assembly.GetExecuting ...
- path类和directory类对文件的路径或目录进行操作
Path: 对文件或目录的路径进行操作(很方便)[只是对字符串的操作] 1.目录和文件操作的命名控件System.IO 2.string Path.ChangeExtension(string ...
- FileInfo类和DirectoryInfo类
FileInfo类和DirectoryInfo类可以方便地对文件和文件夹进行操作. 1. FileInfo类(非静态类) FileInfo类和File类之间许多方法调用都是相同的,但是FileInfo ...
- C# File和Directory类
File和Directory类 作为实用类,File和Directory类都提供了许多方法,用于处理文件系统以及其中的文件和目录.这些是静态方法,涉及移动文件.查询和更新属性并创建FileStream ...
随机推荐
- 手机访问PC网站自动跳转到手机网站代码(转)
4G时代,手机网站已经非常普遍了,一般手机网站都有一个二级域名来访问,比如 m.16css.com 如果手机直接访问www.16css.com 就是PC网站,在手机上浏览电脑版网站体验非常不好. 如果 ...
- UCOSII下外部中断的实现
1.外部中断初始化函数 // EXTI initialize void EXTIX_INIT(void) { EXTI_InitTypeDef EXTI_InitStructure; //定义外部中断 ...
- Lib之过?Java反序列化漏洞通用利用分析
转http://blog.chaitin.com/ 1 背景 2 Java反序列化漏洞简介 3 利用Apache Commons Collections实现远程代码执行 4 漏洞利用实例 4.1 利用 ...
- AngularJS:事件
ylbtech-AngularJS:事件 1.返回顶部 1. AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 Ang ...
- 数据库:ubantu下MySQL安装指南
http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 安装MySQL sudo apt-get install mys ...
- delphi 安卓配置教程
本教程以 delphi 10.2.2.2004 为例,演示 delphi 安卓配置步骤 1.打开 Android Tools 2. 选择合适的版本.比如:我的小米4 LTE 是 andorid 6.0 ...
- Howto Reboot or halt Linux system in emergency (ZT)
http://www.cyberciti.biz/tips/reboot-or-halt-linux-system-in-emergency.html Linux kernel includes ma ...
- UIView显示原理和过程
一.UIView显示原理 一个控件,UIView之所以可以显示,是因为内部在UIView的内部有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内 ...
- JS中,根据div数值判断弹出窗口
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 基于C++求两个数的最大公约数最小公倍数
求x,y最大公约数的函数如下: int gys(int x,int y) { int temp; while(x) {temp=x; x=y%x; y=temp;} return y; } x=y的时 ...