Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Counting Lines of Text
|
577
array must be created and filled with RegexCompilationInfo objects. The next step is to
create the assembly in which the expression will live. An instance of the
AssemblyName
class is created using the default constructor. Next, this assembly is given a name (do
not include the
.dll file extension in the name; it is added automatically). Finally, the
CompileToAssembly method can be called with the RegexCompilationInfo array and the
AssemblyName object supplied as arguments.
In our example, this assembly is placed in the same directory that the
executable was launched from.
See Also
See the “.NET Framework Regular Expressions” and “AssemblyName Class” topics
in the MSDN documentation.
10.9 Counting Lines of Text
Problem
You need to count lines of text within a string or within a file.
Solution
Use the LineCount method shown in Example 10-8 to read in the entire file and count
the number of line feeds.
Example 10-8. LineCount method
using System;
using System.Text.RegularExpressions;
using System.IO;
public static long LineCount(string source, bool isFileName)
{
if (source != null)
{
string text = source;
if (isFileName)
{
using (FileStream FS = new FileStream(source, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
using (StreamReader SR = new StreamReader(FS))
{
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
C# Cookbook

C# Cookbook

Joe Mayo
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata