Sideway
output.to from Sideway
Draft for Information Only

Content

File System
 Directory Class
  Constructors
  Methods
  Remarks
  Applies to
  See also
 File Class
  Methods
  Remarks
  Applies to
  See also
 Examples
 Source/Reference

File System

The main components used in .NET framework to access the file system are DriveInfo, DriveType, FileSystemInfo, DirectoryInfoFileInfo , and Directory, File,

Directory Class

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

NamespaceSystem.IO AssembliesSystem.IO.FileSystem.dll, mscorlib.dll, netstandard.dll
[System.Runtime.InteropServices.ComVisible(true)]
public static class Directory

Inheritance: Object->Directory

Attributes: ComVisibleAttribute

Constructors

Methods

CreateDirectory(String)

Creates all directories and subdirectories in the specified path unless they already exist.

CreateDirectory(String, DirectorySecurity)

Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.

Delete(String)

Deletes an empty directory from a specified path.

Delete(String, Boolean)

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

EnumerateDirectories(String)

Returns an enumerable collection of directory names in a specified path.

EnumerateDirectories(String, String)

Returns an enumerable collection of directory names that match a search pattern in a specified path.

EnumerateDirectories(String, String, SearchOption)

Returns an enumerable collection of directory names that match a search pattern in a specified path, and optionally searches subdirectories.

EnumerateFiles(String, String, SearchOption)

Returns an enumerable collection of file names that match a search pattern in a specified path, and optionally searches subdirectories.

EnumerateFiles(String)

Returns an enumerable collection of file names in a specified path.

EnumerateFiles(String, String)

Returns an enumerable collection of file names that match a search pattern in a specified path.

EnumerateFileSystemEntries(String)

Returns an enumerable collection of file names and directory names in a specified path.

EnumerateFileSystemEntries(String, String)

Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.

EnumerateFileSystemEntries(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.

Exists(String)

Determines whether the given path refers to an existing directory on disk.

GetAccessControl(String)

Gets a DirectorySecurity object that encapsulates the access control list (ACL) entries for a specified directory.

GetAccessControl(String, AccessControlSections)

Gets a DirectorySecurity object that encapsulates the specified type of access control list (ACL) entries for a specified directory.

GetCreationTime(String)

Gets the creation date and time of a directory.

GetCreationTimeUtc(String)

Gets the creation date and time, in Coordinated Universal Time (UTC) format, of a directory.

GetCurrentDirectory()

Gets the current working directory of the application.

GetDirectories(String, String, SearchOption)

Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.

GetDirectories(String)

Returns the names of subdirectories (including their paths) in the specified directory.

GetDirectories(String, String)

Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

GetDirectoryRoot(String)

Returns the volume information, root information, or both for the specified path.

GetFiles(String)

Returns the names of files (including their paths) in the specified directory.

GetFiles(String, String)

Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

GetFiles(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.

GetFileSystemEntries(String)

Returns the names of all files and subdirectories in a specified path.

GetFileSystemEntries(String, String)

Returns an array of file names and directory names that match a search pattern in a specified path.

GetFileSystemEntries(String, String, SearchOption)

Returns an array of all the file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.

GetLastAccessTime(String)

Returns the date and time the specified file or directory was last accessed.

GetLastAccessTimeUtc(String)

Returns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.

GetLastWriteTime(String)

Returns the date and time the specified file or directory was last written to.

GetLastWriteTimeUtc(String)

Returns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last written to.

GetLogicalDrives()

Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".

GetParent(String)

Retrieves the parent directory of the specified path, including both absolute and relative paths.

Move(String, String)

Moves a file or a directory and its contents to a new location.

SetAccessControl(String, DirectorySecurity)

Applies access control list (ACL) entries described by a DirectorySecurity object to the specified directory.

SetCreationTime(String, DateTime)

Sets the creation date and time for the specified file or directory.

SetCreationTimeUtc(String, DateTime)

Sets the creation date and time, in Coordinated Universal Time (UTC) format, for the specified file or directory.

SetCurrentDirectory(String)

Sets the application's current working directory to the specified directory.

SetLastAccessTime(String, DateTime)

Sets the date and time the specified file or directory was last accessed.

SetLastAccessTimeUtc(String, DateTime)

Sets the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.

SetLastWriteTime(String, DateTime)

Sets the date and time a directory was last written to.

SetLastWriteTimeUtc(String, DateTime)

Sets the date and time, in Coordinated Universal Time (UTC) format, that a directory was last written to.

Remarks

  • Use the Directory class for typical operations such as copying, moving, renaming, creating, and deleting directories.

    The static methods of the Directory class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of DirectoryInfo instead, because the security check will not always be necessary.

    If you are performing only one directory-related action, it might be more efficient to use a static Directory method rather than a corresponding DirectoryInfo instance method. Most Directory methods require the path to the directory that you are manipulating.

    Note

    In members that accept a string path parameter, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space (" c:\temp"), the path string isn't trimmed, so the path is considered malformed and an exception is raised. In addition, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception. Ensure that your paths are well-formed when using methods that accept a path string. For more information see Path.

    In members that accept a path, the path can refer to a file or a directory. You can use a full path, a relative path, or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

    • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

    • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

    • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

    By default, full read/write access to new directories is granted to all users. However, the app must have the correct security to access existing directories.

    To demand permissions for a directory and all its subdirectories, end the path string with the directory separator character. (For example, "C:\Temp\" grants access to C:\Temp\ and all its subdirectories.) To demand permissions only for a specific directory, end the path string with a period. (For example, "C:\Temp\." grants access only to C:\Temp\, not to its subdirectories.)

    In members that accept a searchPattern parameter, the search string can be any combination of literal characters and two wildcard characters; * and ?. This parameter does not recognize regular expressions. For more information, see the EnumerateDirectories(String, String) method or any other method that uses the searchPattern parameter.

    For a list of common I/O tasks, see Common I/O Tasks.

    Directory and DirectoryInfo are not supported for use in Windows Store apps. For information about how to access files and folders in Windows Store apps, see Accessing data and files (Windows Store apps).

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

See also

File Class

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

NamespaceSystem.IO AssembliesSystem.IO.FileSystem.dll, mscorlib.dll, netstandard.dll
[System.Runtime.InteropServices.ComVisible(true)]
public static class File

Inheritance: Object->File

Attributes: ComVisibleAttribute

Methods

AppendAllLines(String, IEnumerable<String>)

Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

AppendAllLines(String, IEnumerable<String>, Encoding)

Appends lines to a file by using a specified encoding, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

AppendAllText(String, String)

Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

AppendAllText(String, String, Encoding)

Appends the specified string to the file, creating the file if it does not already exist.

AppendText(String)

Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

Copy(String, String)

Copies an existing file to a new file. Overwriting a file of the same name is not allowed.

Copy(String, String, Boolean)

Copies an existing file to a new file. Overwriting a file of the same name is allowed.

Create(String)

Creates or overwrites a file in the specified path.

Create(String, Int32)

Creates or overwrites the specified file.

Create(String, Int32, FileOptions)

Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file.

Create(String, Int32, FileOptions, FileSecurity)

Creates or overwrites the specified file with the specified buffer size, file options, and file security.

CreateText(String)

Creates or opens a file for writing UTF-8 encoded text. If the file already exists, its contents are overwritten.

Decrypt(String)

Decrypts a file that was encrypted by the current account using the Encrypt(String) method.

Delete(String)

Deletes the specified file.

Encrypt(String)

Encrypts a file so that only the account used to encrypt the file can decrypt it.

Exists(String)

Determines whether the specified file exists.

GetAccessControl(String)

Gets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.

GetAccessControl(String, AccessControlSections)

Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for a particular file.

GetAttributes(String)

Gets the FileAttributes of the file on the path.

GetCreationTime(String)

Returns the creation date and time of the specified file or directory.

GetCreationTimeUtc(String)

Returns the creation date and time, in coordinated universal time (UTC), of the specified file or directory.

GetLastAccessTime(String)

Returns the date and time the specified file or directory was last accessed.

GetLastAccessTimeUtc(String)

Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.

GetLastWriteTime(String)

Returns the date and time the specified file or directory was last written to.

GetLastWriteTimeUtc(String)

Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.

Move(String, String)

Moves a specified file to a new location, providing the option to specify a new file name.

Open(String, FileMode)

Opens a FileStream on the specified path with read/write access with no sharing.

Open(String, FileMode, FileAccess)

Opens a FileStream on the specified path, with the specified mode and access with no sharing.

Open(String, FileMode, FileAccess, FileShare)

Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

OpenRead(String)

Opens an existing file for reading.

OpenText(String)

Opens an existing UTF-8 encoded text file for reading.

OpenWrite(String)

Opens an existing file or creates a new file for writing.

ReadAllBytes(String)

Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

ReadAllLines(String)

Opens a text file, reads all lines of the file, and then closes the file.

ReadAllLines(String, Encoding)

Opens a file, reads all lines of the file with the specified encoding, and then closes the file.

ReadAllText(String, Encoding)

Opens a file, reads all text in the file with the specified encoding, and then closes the file.

ReadAllText(String)

Opens a text file, reads all the text in the file, and then closes the file.

ReadLines(String)

Reads the lines of a file.

ReadLines(String, Encoding)

Read the lines of a file that has a specified encoding.

Replace(String, String, String)

Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file.

Replace(String, String, String, Boolean)

Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors.

SetAccessControl(String, FileSecurity)

Applies access control list (ACL) entries described by a FileSecurity object to the specified file.

SetAttributes(String, FileAttributes)

Sets the specified FileAttributes of the file on the specified path.

SetCreationTime(String, DateTime)

Sets the date and time the file was created.

SetCreationTimeUtc(String, DateTime)

Sets the date and time, in coordinated universal time (UTC), that the file was created.

SetLastAccessTime(String, DateTime)

Sets the date and time the specified file was last accessed.

SetLastAccessTimeUtc(String, DateTime)

Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.

SetLastWriteTime(String, DateTime)

Sets the date and time that the specified file was last written to.

SetLastWriteTimeUtc(String, DateTime)

Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.

WriteAllBytes(String, Byte[])

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

WriteAllLines(String, String[], Encoding)

Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

WriteAllLines(String, IEnumerable<String>, Encoding)

Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.

WriteAllLines(String, String[])

Creates a new file, write the specified string array to the file, and then closes the file.

WriteAllLines(String, IEnumerable<String>)

Creates a new file, writes a collection of strings to the file, and then closes the file.

WriteAllText(String, String)

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

WriteAllText(String, String, Encoding)

Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.

Remarks

  • Use the File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to a single file at a time. You can also use the File class to get and set file attributes or DateTime information related to the creation, access, and writing of a file. If you want to perform operations on multiple files, see Directory.GetFiles or DirectoryInfo.GetFiles.

    Many of the File methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific File members such as OpenText, CreateText, or Create.

    Because all File methods are static, it might be more efficient to use a File method rather than a corresponding FileInfo instance method if you want to perform only one action. All File methods require the path to the file that you are manipulating.

    The static methods of the File class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.

    By default, full read/write access to new files is granted to all users.

    The following table describes the enumerations that are used to customize the behavior of various File methods.

    Enumeration Description
    FileAccess Specifies read and write access to a file.
    FileShare Specifies the level of access permitted for a file that is already in use.
    FileMode Specifies whether the contents of an existing file are preserved or overwritten, and whether requests to create an existing file cause an exception.

    Note

    In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

    In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

    • "c:\\\MyDir\\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

    • "c:\\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

    • "MyDir\\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

    • "\\\\\\\MyServer\\\MyShare" in C#, or "\\\MyServer\MyShare" in Visual Basic.

    For a list of common I/O tasks, see Common I/O Tasks.

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

See also

Examples

Examples of Directory, and File Classes
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <script runat="server" >
           Sub Page_Load()
               Dim xdir, xdir1 As String
               Dim xdirs As Array
               Dim xfile As String
               Dim xfiles As Array
               Dim xdrivestr As String = ""
               System.IO.Directory.SetCurrentDirectory("T:\")
               xdir =  System.IO.Directory.GetCurrentDirectory
               Dim xi As Integer = 0
               xdrivestr = xdrivestr & xdir & "<br />"
               xdirs = System.IO.Directory.GetDirectories(xdir)
               For Each xdir1 in xdirs
                   xdrivestr = xdrivestr & xdir1 & "<br />"
               Next
               xfiles = System.IO.Directory.GetFiles(xdir)
               For Each xfile in xfiles
                   xdrivestr = xdrivestr & xfile & "<br />"
               Next
               lbl01.Text = xdrivestr
           End Sub
       </script>
    </head>
    <body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
       <% Response.Write ("<h1>This is a Sample Page of Directory, and File Classes</h1>") %>
       <p>
           <%-- Set on Page_Load --%>
           <asp:Label id="lbl01" runat="server" />
       </p>
    </body>
</html>
HTML Web Page Embedded Output:

Source/Reference


©sideway

ID: 200700009 Last Updated: 9/7/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 149

Software

Application 198

Digitization 119

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 97

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019