File System FileSystemInfo Class Constructors Fields Properties Methods Security Remarks Applies to See also DirectoryInfo Class Constructors Properties Methods Fields Extension Methods Remarks Applies to See also FileInfo Class Constructors Properties Methods Fields Extension Methods Remarks Applies to See also Examples Source/Reference
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class FileSystemInfo : MarshalByRefObject,
System.Runtime.Serialization.ISerializable
For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. Otherwise, the Name property gets the name of the directory.
FileIOPermission
for permission to inherit from this class. Associated enumeration: Unrestricted Security Action: Inheritance Demand
Remarks
The FileSystemInfo class contains methods that are common to file and directory manipulation. A FileSystemInfo object can represent either a file or a directory, thus serving as the basis for FileInfo or DirectoryInfo objects. Use this base class when parsing a lot of files and directories.
A derived class can inherit from FileSystemInfo only if the derived class has the AllAccess permission from the FileIOPermissionAccess enumeration.
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.
Creates a subdirectory or subdirectories on the specified path with the specified security. The specified path can be relative to this instance of the DirectoryInfo class.
Gets a DirectorySecurity object that encapsulates the access control list (ACL) entries for the directory described by the current DirectoryInfo object.
Gets a DirectorySecurity object that encapsulates the specified type of access control list (ACL) entries for the directory described by the current DirectoryInfo object.
Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.
Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class, because a security check will not always be necessary.
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.
By default, full read/write access to new directories is granted to all users.
Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.
Gets a FileSecurity object that encapsulates the specified type of access control list (ACL) entries for the file described by the current FileInfo object.
Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file.
Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.
Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.
If you are performing multiple operations on the same file, it can be more efficient to use FileInfo instance methods instead of the corresponding static methods of the File class, because a security check will not always be necessary.
Many of the FileInfo 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 FileInfo members such as Open, OpenRead, OpenText,
CreateText, or Create.
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 FileInfo methods.
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.
The FileInfo class provides the following properties that enable you to retrieve information about a file. For an example of how to use each property, see the property pages.
The Directory property retrieves an object that represents the parent directory of a file.
The DirectoryName property retrieves the full path of the parent directory of a file.
The Exists property checks for the presence of a file before operating on it.
The IsReadOnly property retrieves or sets a value that specifies whether a file can be modified.
<!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 System.IO.DirectoryInfo
Dim xdirs As System.IO.Directoryinfo()
Dim xfile As System.IO.FileInfo
Dim xfiles As System.IO.Fileinfo()
Dim xdrivestr As String = ""
xdir = New System.IO.Directoryinfo("T:\")
Dim xi As Integer = 0
xdrivestr = xdrivestr & xdir.FullName & "<br />"
xdirs = xdir.GetDirectories()
For Each xdir1 in xdirs
xdrivestr = xdrivestr & xdir1.FullName & "<br />"
Next
xfiles = xdir.GetFiles()
For Each xfile in xfiles
xdrivestr = xdrivestr & xfile.FullName & "<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 DirectoryInfo, and FileInfo Classes</h1>") %>
<p>
<%-- Set on Page_Load --%>
<asp:Label id="lbl01" runat="server" />
</p>
</body>
</html>