Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# Windows File System Operations

C# Windows File System Operations

C# File Input and Output C# File Input and Output

C# allows you to use various directory- and file-related classes to manipulate directories and files, such as DirectoryInfo class and FileInfo class.

DirectoryInfo class

DirectoryInfo class derives from the FileSystemInfo class. It provides various methods for creating, moving, and browsing directories and subdirectories. This class cannot be inherited.

The following table lists the DirectoryInfo Some commonly used methods in the class:property:

No. Attribute & Description
1 Attributes
Gets the attributes of the current file or directory.
2 CreationTime
Gets the creation time of the current file or directory.
3 Exists
Gets a Boolean value indicating whether the directory exists.
4 Extension
Gets a string representing the existence of a file.
5 FullName
Gets the full path of a directory or file.
6 LastAccessTime
Gets the time when the current file or directory was last accessed.
7 Name
Gets the name of this DirectoryInfo instance.

The following table lists the DirectoryInfo Some commonly used methods in the class:Method:

No. Method & Description
1 public void Create()
Creates a directory.
2 **public DirectoryInfo CreateSubdirectory( string path ) **
Creates a subdirectory at the specified path. The specified path can be relative to the instance of the DirectoryInfo class.
3 public override void Delete()
Deletes this DirectoryInfo if it is empty.
4 public DirectoryInfo[] GetDirectories()
Returns the subdirectories of the current directory.
5 public FileInfo[] GetFiles()
Returns a list of files from the current directory.

For a complete list of properties and methods, visit Microsoft's C# documentation.

FileInfo class

FileInfo class derives from the FileSystemInfo class. It provides properties and methods for creating, copying, deleting, moving, and opening files, and facilitates the creation of FileStream objects. This class cannot be inherited.

The following table lists the FileInfo Some commonly used methods in the class:property:

No. Attribute & Description
1 Attributes
Gets the attributes of the current file.
2 CreationTime
Gets the creation time of the current file.
3 Directory
Gets an instance of the directory to which the file belongs.
4 Exists
Gets a Boolean value indicating whether the file exists.
5 Extension
Gets a string representing the existence of a file.
6 FullName
Gets the full path of the file.
7 LastAccessTime
Gets the time when the current file was last accessed.
8 LastWriteTime
Gets the time when the file was last written to.
9 Length
Gets the size of the current file in bytes.
10 Name
Gets the name of the file.

The following table lists the FileInfo Some commonly used methods in the class:Method:

No. Method & Description
1 public StreamWriter AppendText()
Creates a StreamWriter that appends text to the file represented by the FileInfo instance.
2 public FileStream Create()
Creates a file.
3 public override void Delete()
Permanently deletes a file.
4 **public void MoveTo( string destFileName ) **
Moves a specified file to a new location, with an option to specify a new file name.
5 **public FileStream Open( FileMode mode ) **
Opens a file in the specified mode.
6 **public FileStream Open( FileMode mode, FileAccess access ) **
Opens a file in the specified mode with read, write, or read/write access.
7 **public FileStream Open( FileMode mode, FileAccess access, FileShare share ) **
Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.
8 public FileStream OpenRead()
Creates a read-only FileStream.
9 public FileStream OpenWrite()
Creates a write-only FileStream.

For a complete list of properties and methods, visit Microsoft's C# documentation.

Example

The following example demonstrates the use of the classes mentioned above:

Example

using

System

;

using

System.IO

;

namespace

WindowsFileApplication

{

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

// create a DirectoryInfo object

            DirectoryInfo mydir

=

new

DirectoryInfo

(

@"c:\Windows"

)

;

           

// get the files in the directory and their names and sizes

            FileInfo

[

]

f

=

mydir

.

GetFiles

(

)

;

           

foreach

(

FileInfo file

in

f

)

           

{

                Console

.

WriteLine

(

"File Name: {0} Size: {1}"

,

                                    file

.

Name

, file

.

Length

)

;

           

}

            Console

.

ReadKey

(

)

;

       

}

   

}

}

When you compile and run the above program, it displays the file names and their sizes in the Windows directory.

C# File Input and Output C# File Input and Output