Shortcut Methods from File Class

The following static methods read an entire file into memory in one step:

The following static methods write an entire file in one step:

ReadLines is like ReadAllLines except that it returns a lazily-evaluated IEnumerable<string>. LINQ is ideal for consuming the results.

The following calculates the number of lines greater than 80 characters in length:


using System;
using System.IO;
using System.Linq;
class Program
{
    static void Main()
    {
        int longLines = File.ReadLines("customer.xml").Count(l => l.Length > 80);

    }
}

AppDomain.CurrentDomain.BaseDirectory returns the application base directory.

To specify a filename relative to this directory, you can call Path.Combine.


using System;
using System.IO;
using System.Linq;
class Program
{
    static void Main()
    {
        string baseFolder = AppDomain.CurrentDomain.BaseDirectory;
        string logoPath = Path.Combine(baseFolder, "customer.xml"); 
        Console.WriteLine(File.Exists(logoPath));
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.