Create, Read, Write, Copy, Move and Delete a Text File using C# and VB.NET
2013-06-29 17:41
1296 查看
In this article, we will explore some common text file operations using C# and VB.NET. This article has been requested by a long
time reader of dotnetcurry.com. NET provides various classes like the File and FileInfo, to create, read, write and perform such similar operations on a text file. For example, you can use the Create() method of the File class to create a text file. The same
could also be done using the CreateText() method of the FileInfo class. Similarly these classes also contain functionality to copy, move or delete a file. The trick is to find out when to use which class and for what purpose.
The ‘File’ class contains static methods and performs security checks on all the methods. The FileInfo contains instance methods
and security check is not necessary. So this method is useful if you plan to use the same instance of the class in multiple operations.
However for copying, moving or deleting the files, the static methods are faster. Moreover, the Garbage Collector has less work
to do when you use the File class, since the methods are static.
For reading and writing text files, we have some abstract classes like the TextReader and TextWriter classes. There are some
useful classes like the StreamReader, StringReader, StreamWriter and StringWriter that derive from these abstract classes.
This article is by no means a comprehensive guide to demonstrate the capabilities of these classes. It should be treated as a
‘How do I’ kind of a series to demonstrate some of the capabilities of these classes. I will be giving some references at the end of this article, which should be read to gain a better understanding of these classes. Let us now see some code which demonstrates
how to use these classes.
Before you go ahead and use these methods, declare a class variable called ‘fileLoc’ which contains the filepath of the text
file.
C#
string fileLoc =
@"c:\sample1.txt";
VB.NET
Dim fileLoc
As String = "c:\sample1.txt"
[align=justify]You would also need to reference the System.IO namespace in your project.[/align]
Create a Text File
C#
// Create a Text File
private
void btnCreate_Click(object sender,
EventArgs e)
{
FileStream fs =
null;
if (!File.Exists(fileLoc))
{
using (fs =
File.Create(fileLoc))
{
}
}
}
VB.NET
' Create a Text File
Private
Sub btnCreate_Click(ByVal sender
As Object,
ByVal e As EventArgs)
Dim fs
As FileStream = Nothing
If (Not File.Exists(fileLoc))
Then
fs = File.Create(fileLoc)
Using fs
End
Using
End
If
End
Sub
Write to a Text File
C#
// Write to a Text File
private
void btnWrite_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
using (StreamWriter sw =
new StreamWriter(fileLoc))
{
sw.Write("Some sample text for the file");
}
}
}
VB.NET
' Write to a Text File
Private
Sub btnWrite_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
Using sw
As StreamWriter = New StreamWriter(fileLoc)
sw.Write("Some sample text for the file")
End
Using
End
If
End
Sub
Read From a Text File
C#
// Read From a Text File
private
void btnRead_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
using (TextReader tr =
new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadLine());
}
}
}
VB.NET
' Read From a Text File
Private
Sub btnRead_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
Using tr
As TextReader = New StreamReader(fileLoc)
MessageBox.Show(tr.ReadLine())
End
Using
End
If
End
Sub
Copy a Text File
C#
// Copy a Text File
private
void btnCopy_Click(object sender,
EventArgs e)
{
string fileLocCopy =
@"d:\sample1.txt";
if (File.Exists(fileLoc))
{
// If file already exists in destination, delete it.
if (File.Exists(fileLocCopy))
File.Delete(fileLocCopy);
File.Copy(fileLoc, fileLocCopy);
}
}
VB.NET
' Copy a Text File
Private
Sub btnCopy_Click(ByVal sender
As Object,
ByVal e As EventArgs)
Dim fileLocCopy
As String = "d:\sample1.txt"
If File.Exists(fileLoc)
Then
' If file already exists in destination, delete it.
If File.Exists(fileLocCopy)
Then
File.Delete(fileLocCopy)
End
If
File.Copy(fileLoc, fileLocCopy)
End
If
End
Sub
Move a Text File
C#
// Move a Text file
private
void btnMove_Click(object sender,
EventArgs e)
{
// Create unique file name
string fileLocMove =
@"d:\sample1" + System.DateTime.Now.Ticks +
".txt";
if (File.Exists(fileLoc))
{
File.Move(fileLoc, fileLocMove);
}
}
VB.NET
' Move a Text file
Private
Sub btnMove_Click(ByVal sender
As Object,
ByVal e As EventArgs)
' Create unique file name
Dim fileLocMove
As String = "d:\sample1" & System.DateTime.Now.Ticks & ".txt"
If File.Exists(fileLoc)
Then
File.Move(fileLoc, fileLocMove)
End
If
End
Sub
Delete a Text File
C#
// Delete a text file
private
void btnDelete_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
File.Delete(fileLoc);
}
}
VB.NET
' Delete a text file
Private
Sub btnDelete_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
File.Delete(fileLoc)
End
If
End
Sub
We saw how to achieve some of the most common requirements while dealing with text files. Take a look at these reference links
below to get a detailed understanding of the File and FileInfo class.
References :
File Class
FileInfo Class
File System and Registry Guide
[align=justify]I hope this article was useful and I thank you for viewing it.[/align]
time reader of dotnetcurry.com. NET provides various classes like the File and FileInfo, to create, read, write and perform such similar operations on a text file. For example, you can use the Create() method of the File class to create a text file. The same
could also be done using the CreateText() method of the FileInfo class. Similarly these classes also contain functionality to copy, move or delete a file. The trick is to find out when to use which class and for what purpose.
The ‘File’ class contains static methods and performs security checks on all the methods. The FileInfo contains instance methods
and security check is not necessary. So this method is useful if you plan to use the same instance of the class in multiple operations.
However for copying, moving or deleting the files, the static methods are faster. Moreover, the Garbage Collector has less work
to do when you use the File class, since the methods are static.
For reading and writing text files, we have some abstract classes like the TextReader and TextWriter classes. There are some
useful classes like the StreamReader, StringReader, StreamWriter and StringWriter that derive from these abstract classes.
This article is by no means a comprehensive guide to demonstrate the capabilities of these classes. It should be treated as a
‘How do I’ kind of a series to demonstrate some of the capabilities of these classes. I will be giving some references at the end of this article, which should be read to gain a better understanding of these classes. Let us now see some code which demonstrates
how to use these classes.
Before you go ahead and use these methods, declare a class variable called ‘fileLoc’ which contains the filepath of the text
file.
C#
string fileLoc =
@"c:\sample1.txt";
VB.NET
Dim fileLoc
As String = "c:\sample1.txt"
[align=justify]You would also need to reference the System.IO namespace in your project.[/align]
Create a Text File
C#
// Create a Text File
private
void btnCreate_Click(object sender,
EventArgs e)
{
FileStream fs =
null;
if (!File.Exists(fileLoc))
{
using (fs =
File.Create(fileLoc))
{
}
}
}
VB.NET
' Create a Text File
Private
Sub btnCreate_Click(ByVal sender
As Object,
ByVal e As EventArgs)
Dim fs
As FileStream = Nothing
If (Not File.Exists(fileLoc))
Then
fs = File.Create(fileLoc)
Using fs
End
Using
End
If
End
Sub
Write to a Text File
C#
// Write to a Text File
private
void btnWrite_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
using (StreamWriter sw =
new StreamWriter(fileLoc))
{
sw.Write("Some sample text for the file");
}
}
}
VB.NET
' Write to a Text File
Private
Sub btnWrite_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
Using sw
As StreamWriter = New StreamWriter(fileLoc)
sw.Write("Some sample text for the file")
End
Using
End
If
End
Sub
Read From a Text File
C#
// Read From a Text File
private
void btnRead_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
using (TextReader tr =
new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadLine());
}
}
}
VB.NET
' Read From a Text File
Private
Sub btnRead_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
Using tr
As TextReader = New StreamReader(fileLoc)
MessageBox.Show(tr.ReadLine())
End
Using
End
If
End
Sub
Copy a Text File
C#
// Copy a Text File
private
void btnCopy_Click(object sender,
EventArgs e)
{
string fileLocCopy =
@"d:\sample1.txt";
if (File.Exists(fileLoc))
{
// If file already exists in destination, delete it.
if (File.Exists(fileLocCopy))
File.Delete(fileLocCopy);
File.Copy(fileLoc, fileLocCopy);
}
}
VB.NET
' Copy a Text File
Private
Sub btnCopy_Click(ByVal sender
As Object,
ByVal e As EventArgs)
Dim fileLocCopy
As String = "d:\sample1.txt"
If File.Exists(fileLoc)
Then
' If file already exists in destination, delete it.
If File.Exists(fileLocCopy)
Then
File.Delete(fileLocCopy)
End
If
File.Copy(fileLoc, fileLocCopy)
End
If
End
Sub
Move a Text File
C#
// Move a Text file
private
void btnMove_Click(object sender,
EventArgs e)
{
// Create unique file name
string fileLocMove =
@"d:\sample1" + System.DateTime.Now.Ticks +
".txt";
if (File.Exists(fileLoc))
{
File.Move(fileLoc, fileLocMove);
}
}
VB.NET
' Move a Text file
Private
Sub btnMove_Click(ByVal sender
As Object,
ByVal e As EventArgs)
' Create unique file name
Dim fileLocMove
As String = "d:\sample1" & System.DateTime.Now.Ticks & ".txt"
If File.Exists(fileLoc)
Then
File.Move(fileLoc, fileLocMove)
End
If
End
Sub
Delete a Text File
C#
// Delete a text file
private
void btnDelete_Click(object sender,
EventArgs e)
{
if (File.Exists(fileLoc))
{
File.Delete(fileLoc);
}
}
VB.NET
' Delete a text file
Private
Sub btnDelete_Click(ByVal sender
As Object,
ByVal e As EventArgs)
If File.Exists(fileLoc)
Then
File.Delete(fileLoc)
End
If
End
Sub
We saw how to achieve some of the most common requirements while dealing with text files. Take a look at these reference links
below to get a detailed understanding of the File and FileInfo class.
References :
File Class
FileInfo Class
File System and Registry Guide
[align=justify]I hope this article was useful and I thank you for viewing it.[/align]
相关文章推荐
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Read and Write compressed data to a binary file using ASP.NET
- AsyncFileUpload File Type and File Size Validation using VB.NET
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- using vb.net export a datatable to Excel and save as file
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- .NET FileUploader(C# and VB)[ASP.NET 大文件上传组件感冒奉献版]
- Sending Emails Through Outlook using C# and VB.NET
- [Training Video - 6] [File Reading] [Java] Create and Write Excel File Using Apache POI API
- C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net
- Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]
- C# modify file (read all and modify and write all)
- CreateFile ReadFile WriteFile 详解
- VB.NET and C# 语法比较手册
- Read and write excel file with format by pandas and xlwt libraries of python
- python.pandas read and write CSV file