有关C#中的文件操作之目录管理 个人整理

这里我们主要用到Directory这个类来进行目录管理的
利用它,我们可以完成对目录的及其子目录的进行创建、移动、浏览等操作,甚至还可以定义隐藏目录和只读目录。
一、目录管理
其中参数path表示目录所在路径。
Directory的主要属性有:
1.Attributes:0X01表示只读,0X02表示隐藏
2.Name:当前路径名
3.Parent:上一级父目录名
4.Root:所在根目录名
5.CreationTime:目录创建时间
6.LastAccessTime:上次访问目录的时间
7.LastWriteTime:上次修改目录的时间
我们常常用到的一些Directory中的一些成员方法:
CreateDirectory(string path) 创建子目录
CreateDiectories(stirng path) 创建多级子目录
CreateFile(string filename) 当前目录下创建一个新文件
Delete() 删除目录

CreateDirectory已重载。 创建指定路径中的所有目录。
Delete已重载。 删除指定的目录。
Exists确定给定路径是否引用磁盘上的现有目录。
GetAccessControl已重载。 返回某个目录的 Windows 访问控制列表 (ACL)。
GetCreationTime获取目录的创建日期和时间。
GetCreationTimeUtc获取目录创建的日期和时间,其格式为协调通用时间 (UTC)。
GetCurrentDirectory获取应用程序的当前工作目录。
GetDirectories已重载。 获取指定目录中子目录的名称。
GetDirectoryRoot返回指定路径的卷信息、根信息或两者同时返回。
GetFiles已重载。 返回指定目录中的文件的名称。
GetFileSystemEntries已重载。 返回指定目录中所有文件和子目录的名称。
GetLastAccessTime返回上次访问指定文件或目录的日期和时间。
GetLastAccessTimeUtc返回上次访问指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。
GetLastWriteTime返回上次写入指定文件或目录的日期和时间。
GetLastWriteTimeUtc返回上次写入指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。
GetLogicalDrives检索此计算机上格式为“<驱动器号>:\”的逻辑驱动器的名称。
GetParent检索指定路径的父目录,包括绝对路径和相对路径。
Move将文件或目录及其内容移到新位置。
SetAccessControl将 DirectorySecurity 对象描述的访问控制列表 (ACL) 项应用于指定的目录。
SetCreationTime为指定的文件或目录设置创建日期和时间。
SetCreationTimeUtc设置指定文件或目录的创建日期和时间,其格式为协调通用时间 (UTC)。
SetCurrentDirectory将应用程序的当前工作目录设置为指定的目录。
SetLastAccessTime设置上次访问指定文件或目录的日期和时间。
SetLastAccessTimeUtc设置上次访问指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。
SetLastWriteTime设置上次写入目录的日期和时间。
SetLastWriteTimeUtc设置上次写入某个目录的日期和时间,其格式为协调通用时间 (UTC)。


Examples:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\MyDir";
        string target = @"c:\TestDir";

        try 
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path)) 
            {
                // Create the directory it does not exist.
                Directory.CreateDirectory(path);
            }

            if (Directory.Exists(target)) 
            {
                // Delete the target to ensure it is not there.
                Directory.Delete(target, true);
            }

            // Move the directory.
            Directory.Move(path, target);

            // Create a file in the directory.
            File.CreateText(target + @"\myfile.txt");

            // Count the files in the target directory.
            Console.WriteLine("The number of files in {0} is {1}",
                target, Directory.GetFiles(target).Length);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

// The following example calculates the size of a directory
// and its subdirectories, if any, and displays the total size
// in bytes.

using System;
using System.IO;

public class ShowDirSize 
{
    public static long DirSize(DirectoryInfo d) 
    {    
        long Size = 0;    
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis) 
        {      
            Size += fi.Length;    
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis) 
        {
            Size += DirSize(di);   
        }
        return(Size);  
    }
    public static void Main(string[] args) 
    {
        if (args.Length != 1) 
        {
            Console.WriteLine("You must provide a directory argument at the command line.");    
        } 
        else 
        {  
            DirectoryInfo d = new DirectoryInfo(args[0]);
            Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d));
        }
    }
}


该类是一个静态类,只能通过.方法名()来调用
不能用其来建对象,也就是不能用其来进行实例化


二、文件管理
File类通常和FileStream类协作来完成对文件的创建、删除、拷贝、移动、打开等操作

同样File类也有自己的属性,如绝对路径名DeriectoryName、创建时间CreationTime、上次访问时间LastAccessTime、上次修改时间LastWriteTime、文件长度Length等
AppendAllText已重载。 将指定的字符串追加到文件中,如果文件还不存在则创建该文件。
AppendText创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。
Copy已重载。 将现有文件复制到新文件。
Create已重载。 在指定路径中创建文件。
CreateText创建或打开一个文件用于写入 UTF-8 编码的文本。
Decrypt解密由当前帐户使用 Encrypt 方法加密的文件。
Delete删除指定的文件。如果指定的文件不存在,则不引发异常。
Encrypt将某个文件加密,使得只有加密该文件的帐户才能将其解密。
Exists确定指定的文件是否存在。
GetAccessControl已重载。 获取一个 FileSecurity 对象,它封装指定文件的访问控制列表 (ACL) 条目。
GetAttributes获取在此路径上的文件的 FileAttributes
GetCreationTime返回指定文件或目录的创建日期和时间。
GetCreationTimeUtc返回指定的文件或目录的创建日期及时间,其格式为协调通用时间 (UTC)。
GetLastAccessTime返回上次访问指定文件或目录的日期和时间。
GetLastAccessTimeUtc返回上次访问指定的文件或目录的日期及时间,其格式为协调通用时间 (UTC)。
GetLastWriteTime返回上次写入指定文件或目录的日期和时间。
GetLastWriteTimeUtc返回上次写入指定的文件或目录的日期和时间,其格式为协调通用时间 (UTC)。
Move将指定文件移到新位置,并提供指定新文件名的选项。
Open已重载。 打开指定路径上的 FileStream
OpenRead打开现有文件以进行读取。
OpenText打开现有 UTF-8 编码文本文件以进行读取。
OpenWrite打开现有文件以进行写入。
ReadAllBytes打开一个文件,将文件的内容读入一个字符串,然后关闭该文件。
ReadAllLines已重载。 打开一个文本文件,将文件的所有行都读入一个字符串数组,然后关闭该文件。
ReadAllText已重载。 打开一个文本文件,将文件的所有行读入一个字符串,然后关闭该文件。
Replace已重载。 使用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。
 SetAccessControl对指定的文件应用由 FileSecurity 对象描述的访问控制列表 (ACL) 项。
 SetAttributes设置指定路径上文件的指定的 FileAttributes
 SetCreationTime设置创建该文件的日期和时间。
 SetCreationTimeUtc设置文件创建的日期和时间,其格式为协调通用时间 (UTC)。
 SetLastAccessTime设置上次访问指定文件的日期和时间。
 SetLastAccessTimeUtc设置上次访问指定的文件的日期和时间,其格式为协调通用时间 (UTC)。
 SetLastWriteTime设置上次写入指定文件的日期和时间。
 SetLastWriteTimeUtc设置上次写入指定的文件的日期和时间,其格式为协调通用时间 (UTC)。
 WriteAllBytes创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。如果目标文件已存在,则覆盖该文件。
 WriteAllLines已重载。 创建一个新文件,在其中写入指定的字符串,然后关闭文件。如果目标文件已存在,则覆盖该文件。
 WriteAllText已重载。 创建一个新文件,在文件中写入内容,然后关闭文件。如果目标文件已存在,则覆盖该文件。

Examples

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path)) 
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }	
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }

        try 
        {
            string path2 = path + "temp";
            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值