C# File Handling PDF
C# File Handling PDF
C# File Handling PDF
Go
C# File C#
► C# Lambda Expressions
► Stream C#
StreamReader. For text files, StreamReader ► Net C# Word
and StreamWriter are often the most useful
types. We use StreamReader in a using block,
a special syntax form.
StreamReader
StringWriter
ReadLine
Based on:
.NET 4.5
using System.IO;
class Program
{
static void Main()
{
// Read every line in the file.
using (StreamReader reader = new StreamReader("file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Do something with the line.
string[] parts = line.Split(',');
}
}
}
}
http://www.dotnetperls.com/file 1/8
10/27/2014 C# File Handling
using System;
using System.IO;
class Program
{
static void Main()
{
string file = File.ReadAllText("C:\\file.txt");
Console.WriteLine(file);
}
}
ReadAllLines. Here we read all the lines from a file and place them
in an array. The code reads lines from "file.txt" and uses a foreach-
http://www.dotnetperls.com/file 2/8
10/27/2014 C# File Handling
using System.IO;
class Program
{
static void Main()
{
// Read in every line in specified file.
// ... This will store all lines in an array in memory.
string[] lines = File.ReadAllLines("file.txt");
foreach (string line in lines)
{
// Do something with the line.
if (line.Length > 80)
{
// Important code.
}
}
}
}
using System.IO;
class Program
{
static void Main()
{
// Another method of counting lines in a file.
// ... This is not the most efficient way.
// ... It counts empty lines.
int lineCount = File.ReadAllLines("file.txt").Length;
}
}
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
http://www.dotnetperls.com/file 3/8
10/27/2014 C# File Handling
// See if line exists in a file.
// ... Uses a query expression to count matching lines.
// ... If one matches, exists is set to true.
bool exists = (from line in File.ReadAllLines("file.txt")
where line == "Some line match"
select line).Count() > 0;
}
}
using System.IO;
class Program
{
static void Main()
{
// Write a string array to a file.
string[] stringArray = new string[]
{
"cat",
"dog",
"arrow"
};
File.WriteAllLines("file.txt", stringArray);
}
}
Results: file.txt
cat
dog
arrow
using System.IO;
class Program
{
static void Main()
{
File.WriteAllText("C:\\perls.txt",
"Dot Net Perls");
}
http://www.dotnetperls.com/file 4/8
10/27/2014 C# File Handling
}
ReadAllBytes. We use
File.ReadAllBytes to read an image (a
PNG) into memory. With this code, we
could cache an image in memory. It
outperforms reading the image in
each time.
File.ReadAllBytes
File.WriteAllBytes: Compress
http://www.dotnetperls.com/file 5/8
10/27/2014 C# File Handling
We demonstrate,
and benchmark,
this method. It is useful with large
binary files.
Seek
http://www.dotnetperls.com/file 6/8
10/27/2014 C# File Handling
We can build small and fast storage, or large and slow storage,
but not storage that is both large and fast.
Compilers: Principles, Techniques and Tools
File handling is hard. Even with the helpful types provided in the
.NET Framework, it is fraught with errors. We must account for disk
errors and invalid data. Testing is essential.
http://www.dotnetperls.com/file 7/8
10/27/2014 C# File Handling
► Download File
► C# Open File
► C# Download
http://www.dotnetperls.com/file 8/8