目录与文件管理--System.IO
就去学习吧
全部回复
仅看楼主
level 6
344791576 楼主
1. 创建并删除指定目录
using System;
using System.IO;
class Test
{
public static void Main(string[] args)
{
//指定要操作的目录
string path = "c:\\MyDir";
try
{
//确定目录是否存在
if (Directory.Exists(path))
{
Console.WriteLine("目录已存在");
return;
}
//创建目录
Directory.CreateDirectory(path);
Console.WriteLine("创建目录成功!");
Console.WriteLine("是否删除?Y or N");
string str = Console.ReadLine();
if (str.ToUpper() == "Y")
{
Directory.Delete(path);
Console.WriteLine("删除成功!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
2. 目录的移动
using System;
using System.IO;
using System.Security;
namespace GetFileSystemEntries
{
class Test
{
static void Main(string[] args)
{
Test test = new Test();
string path = Directory.GetCurrentDirectory();
string filter = "*.exe";
test.PrintFileSystemEntries(path);
test.PrintFileSystemEntries(path,filter);
test.GetLogicalDrives();
test.GetParent(path);
test.Move("c:\\2", "c:\\3\\2");//把2移动到3下不能写成test.Move("c:\\2", "c:\\3")
test.Move("c:\\2", "c:\\3");//作用是把2改名成3
}
//显示文件系统目录路径
void PrintFileSystemEntries(string path)
{
try
{
//获取文件系统目录路径
string[] directoryEntries = Directory.GetFileSystemEntries(path);
foreach (string tmpDir in directoryEntries)
{
Console.WriteLine(tmpDir);
}
}
catch (ArgumentNullException)
{
Console.WriteLine("路径为空");
}
catch (SecurityException)
{
Console.WriteLine("检测到安全性错误");
}
catch (ArgumentException)
{
Console.WriteLine("路径是一个零长度的字符串");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("指定的路径无效");
}
}
//方法重载
void PrintFileSystemEntries(string path, string filter)
{
try
{
string[] directoryEntries = Directory.GetFileSystemEntries(path, filter);
foreach (string tmp in directoryEntries)
{
Console.WriteLine(tmp);
}
}
catch (ArgumentNullException)
{
Console.WriteLine("路径为空");
}
catch (SecurityException)
{
Console.WriteLine("检测到安全性错误");
}
catch (ArgumentException)
{
Console.WriteLine("路径是一个零长度的字符串");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("指定的路径无效");
}
}
// 显示所有的逻辑驱动器
void GetLogicalDrives()
{
try
{
string[] drives = Directory.GetLogicalDrives();
foreach (string tmp in drives)
{
Console.WriteLine(tmp);
}
}
catch (IOException)
{
Console.WriteLine("输入输出异常");
}
catch (SecurityException)
{
Console.WriteLine("检测到安全性错误");
}
}
void GetParent(string path)
{
try
{
DirectoryInfo directoryInfo = Directory.GetParent(path);
Console.WriteLine(directoryInfo.FullName);
}
catch (ArgumentNullException)
{
Console.WriteLine("路径为空");
}
catch (ArgumentException)
{
Console.WriteLine("路径是一个零长度的字符串");
}
}
//移动目录
void Move(string sourcePath, string destinationPath)
{
try
{
Directory.Move(sourcePath, destinationPath);
Console.WriteLine("移动目录成功");
}
catch (ArgumentNullException)
{
Console.WriteLine("路径为空");
}
catch (SecurityException)
{
Console.WriteLine("检测到安全性错误");
}
catch (ArgumentException)
{
Console.WriteLine("路径是一个零长度的字符串");
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("指定的路径无效");
}
catch (IOException)
{
Console.WriteLine("试图将一个目录移到已存在的卷或目标");
}
}
}
}

2010年12月13日 06点12分 1
1