C#
string fileLoc = @"c:\sample1.txt";
VB.NET
Dim fileLoc As String = "c:\sample1.txt"
You would also need to reference the System.IO namespace in your project.
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.
No comments:
Post a Comment