C#常用目录操作总结

在C#中,常用的目录操作类有Directory, DirectoryInfo,基本上两个类提供的方法大同小异,有不算太大的区别,但是Directory类是静态类,所有的函数都是静态函数,可以直接调用。


使用Directory可以创建指定的目录,删除目录,获取目录信息,例如创建时间等,获取目录中的子目录,获取目录中的文件集合等操作,还可以设置目录的访问时间等。


简单的一个目录操作的代码,判断目录是否存在,如果不存在则创建目录:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\watkins\Desktop\TcpListener\TcpListener\bin";
            bool a = Directory.Exists(path);
            if (!a)
            {
                Directory.CreateDirectory(path);
            }
            Console.WriteLine(a);
            Console.Read();
        }
    }
}


Directory类的成员列表:


Directory MembersVisual Studio 2010

Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

The Directory type exposes the following members.

Methods

 NameDescription
Public methodStatic memberSupported by the XNA FrameworkCreateDirectory(String)Creates all directories and subdirectories as specified by path.
Public methodStatic memberCreateDirectory(String, DirectorySecurity)Creates all the directories in the specified path, applying the specified Windows security.
Public methodStatic memberSupported by the XNA FrameworkDelete(String)Deletes an empty directory from a specified path.
Public methodStatic memberSupported by the XNA FrameworkDelete(String, Boolean)Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
Public methodStatic memberEnumerateDirectories(String)Returns an enumerable collection of directory names in a specified path.
Public methodStatic memberEnumerateDirectories(String, String)Returns an enumerable collection of directory names that match a search pattern in a specified path.
Public methodStatic memberEnumerateDirectories(String, String, SearchOption)Returns an enumerable collection of directory names that match a search pattern in a specified path, and optionally searches subdirectories.
Public methodStatic memberEnumerateFiles(String)Returns an enumerable collection of file names in a specified path.
Public methodStatic memberEnumerateFiles(String, String)Returns an enumerable collection of file names that match a search pattern in a specified path.
Public methodStatic memberEnumerateFiles(String, String, SearchOption)Returns an enumerable collection of file names that match a search pattern in a specified path, and optionally searches subdirectories.
Public methodStatic memberEnumerateFileSystemEntries(String)Returns an enumerable collection of file-system entries in a specified path.
Public methodStatic memberEnumerateFileSystemEntries(String, String)Returns an enumerable collection of file-system entries that match a search pattern in a specified path.
Public methodStatic memberEnumerateFileSystemEntries(String, String, SearchOption)Returns an enumerable collection of file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.
Public methodStatic memberSupported by the XNA FrameworkExistsDetermines whether the given path refers to an existing directory on disk.
Public methodStatic memberGetAccessControl(String)Gets a DirectorySecurity object that encapsulates the access control list (ACL) entries for a specified directory.
Public methodStatic memberGetAccessControl(String, AccessControlSections)Gets a DirectorySecurity object that encapsulates the specified type of access control list (ACL) entries for a specified directory.
Public methodStatic memberSupported by the XNA FrameworkGetCreationTimeGets the creation date and time of a directory.
Public methodStatic memberGetCreationTimeUtcGets the creation date and time, in Coordinated Universal Time (UTC) format, of a directory.
Public methodStatic memberSupported by the XNA FrameworkGetCurrentDirectoryGets the current working directory of the application.
Public methodStatic memberSupported by the XNA FrameworkGetDirectories(String)Gets the names of subdirectories in the specified directory.
Public methodStatic memberSupported by the XNA FrameworkGetDirectories(String, String)Gets an array of directories matching the specified search pattern from the current directory.
Public methodStatic memberGetDirectories(String, String, SearchOption)Gets an array of directories matching the specified search pattern from the current directory, using a value to determine whether to search subdirectories.
Public methodStatic memberSupported by the XNA FrameworkGetDirectoryRootReturns the volume information, root information, or both for the specified path.
Public methodStatic memberSupported by the XNA FrameworkGetFiles(String)Returns the names of files (including their paths) in the specified directory.
Public methodStatic memberSupported by the XNA FrameworkGetFiles(String, String)Returns the names of files (including their paths) that match the specified search pattern in the specified directory.
Public methodStatic memberGetFiles(String, String, SearchOption)Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
Public methodStatic memberSupported by the XNA FrameworkGetFileSystemEntries(String)Returns the names of all files and subdirectories in the specified directory.
Public methodStatic memberSupported by the XNA FrameworkGetFileSystemEntries(String, String)Returns an array of file system entries that match the specified search criteria.
Public methodStatic memberGetFileSystemEntries(String, String, SearchOption)Gets an array of all the file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.
Public methodStatic memberSupported by the XNA FrameworkGetLastAccessTimeReturns the date and time the specified file or directory was last accessed.
Public methodStatic memberGetLastAccessTimeUtcReturns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.
Public methodStatic memberSupported by the XNA FrameworkGetLastWriteTimeReturns the date and time the specified file or directory was last written to.
Public methodStatic memberGetLastWriteTimeUtcReturns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last written to.
Public methodStatic memberGetLogicalDrivesRetrieves the names of the logical drives on this computer in the form "<drive letter>:\".
Public methodStatic memberGetParentRetrieves the parent directory of the specified path, including both absolute and relative paths.
Public methodStatic memberSupported by the XNA FrameworkMoveMoves a file or a directory and its contents to a new location.
Public methodStatic memberSetAccessControlApplies access control list (ACL) entries described by a DirectorySecurity object to the specified directory.
Public methodStatic memberSetCreationTimeSets the creation date and time for the specified file or directory.
Public methodStatic memberSetCreationTimeUtcSets the creation date and time, in Coordinated Universal Time (UTC) format, for the specified file or directory.
Public methodStatic memberSupported by the XNA FrameworkSetCurrentDirectorySets the application's current working directory to the specified directory.
Public methodStatic memberSetLastAccessTimeSets the date and time the specified file or directory was last accessed.
Public methodStatic memberSetLastAccessTimeUtcSets the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.
Public methodStatic memberSetLastWriteTimeSets the date and time a directory was last written to.
Public methodStatic memberSetLastWriteTimeUtcSets the date and time, in Coordinated Universal Time (UTC) format, that a directory was last written to.
Top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值