File

CreateCreates and opens a file returning a FileStream object
OpenOpens a file with a specified mode, access and share returning a FileStream object
OpenReadOpens a file in read mode returning a FileStream object
OpenWriteOpens a file in write mode returning a FileStream object
OpenTextOpens a text file for reading returning a StreamReader object
CreateTextCreates a text file returning a StreamWriter object
AppendTextOpens a text file in append mode returning a StreamWriter object
GetAttributesReturns the attributes of a file
SetAttributesSets the attributes for a file
CopyCopies a file to a new destination
DeleteDeletes a file
MoveMoves a file
ExistsReturns True if the file exists
GetCreationTimeReturns the creation time of the file
SetCreatiionTimeSets the creation time of the file
GetLastAccessTimeReturns the last access time of the file
SetLastAccessTimeSets the last access time of the file
GetLastWriteTimeReturns the last write time of a file
SetLastWriteTimeSets the last write time of the file

Open

Opens a file with a specified mode, access and share returning a FileStream object
The sharing mode lets you control whether or not other processes can read from or write to the file while you have it open.
A lot of the arguments correspond to overloads of the FileStream constructor.
This method gives you greater (than the Create, OpenRead and OpenWrite methods) when creating new files.
If you want total control when opening and creating files then you should use the FileStream constructor.

System.IO.File.Open(file [,mode [,access [,share ]]]) 
System.IO.File.Open("C:\temp\file1.txt", _
                    FileMode.Append | Create | CreateNew | Open | OpenOrCreate | Truncate, _
                    FileAccess.Read | Write | ReadWrite, _
                    FileShare.None | ReadWrite | Read | Write | Inheritable

OpenRead

Opens an existing file in read mode returning a FileStream object
The file is opened using the default sharing mode
The FileStream is positioned at the beginning of the file.

System.IO.File.OpenRead(file) 

Dim objFileStream As System.IO.FileStream 
objFileStream = System.IO.File.OpenRead("C:\Temp\textfile.txt")

OpenWrite

Opens a file in write mode returning a FileStream object
Any existing data in the file will be overwritten.
If the file you are trying to open does not exist then it will be created.

System.IO.File.OpenWrite(file) 

OpenText

Opens a text file for reading returning a StreamReader object
This method returns a StreamReader that is positioned at the beginning of the file.

System.IO.File.OpenText(file) 

CreateText

Creates a text file and opens it for reading. This returns a StreamWriter object.
If the file you want to create already exists then it is overwritten.

System.IO.File.CreateText(file) 

AppendText

This method can be used if you want to append data to a text file.
Opens an existing text file in append mode returning a StreamWriter object
If the file you want to open does not exist then a new file is created.

System.IO.File.AppendText(file) 

Copy

Copies a file from one location to another.
This method will fail if the file already exists and you have not explicitly stated to overwrite the file.
You also have the option to overwrite the file it it already exists.

System.IO.File.Copy(source, dest [,overwrite]) 
System.IO.File.Copy("file1.txt","file2.txt",False)
System.IO.File.Copy("file1.txt","file2.txt",True)

Delete

Deletes an existing file
This method will throw an exception if the file is in use or if you do not have access (or permission) to delete it.
It will not throw an exception if the file does not exist.

System.IO.File.Delete(path) 

GetCreationTime

Returns a DateTime telling you when the file was created.

System.IO.File.GetCreationTime(path) 

SetCreatiionTime


System.IO.File.SetCreatiionTime(path, datetime) 

GetLastAccessTime


System.IO.File.GetLastAccessTime(path) 

SetLastAccessTime


System.IO.File.SetLastAccessTime(path, datetime) 

GetLastWriteTime


System.IO.File.GetLastWriteTime(path) 

SetLastWriteTime


System.IO.File.SetLastWriteTime(path, datetime) 


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext