Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Example

Result

Code Block
languagec#
    static  void    Main( string[] args )
    {
        Console.WriteLine(File.ReadAllText("C:\\tmp\\Ogre.log"));
    }

Code Block
languagec#
    static  void    Main( string[] args )
    {
        Console.WriteLine(File.ReadAllText(@"C:\tmp\Ogre.log"));
    }

Code Block
    static  void    Main( string[] args )
    {
        Console.WriteLine(File.ReadAllText("C:/tmp/Ogre.log"));
    }

The simplest File program you could write is shown here.

Notice the use of double \\ character. This is so that the oblique (back slash) character is treated as the literal character and not an escape character.

This simple program will read all the ASCII characters from the given text file and display them on the screen.

Alternatively you can use the @ character before the string literal to ensure that each character in the string is treated as a literal value and not translated

If you want to make your code portable across multiple platforms (Windows and Linux/Unix) avoid using the oblique character and replace it with a forward slash as shown here.

Code Block
    IEnumerable<string> lines = File.ReadLines("C:/tmp/Ogre.log");
    foreach (string line in lines)
    {
        Console.WriteLine(line);
    }

In this example ReadLines() reads all the lines (each line is delimited by a CR/LF sequence) into a collection that can be used as

Code Block
    static void Main(string[] args)
    {
        string msg = "Hello world from C#";
        File.WriteAllText("C:/tmp/csharp-file.txt", msg);
    }

This simple application will write the ASCII characters to a file called “C:/tmp/csharp-file.txt”

After running this snippet of code, open the file csharp-file.txt, and its contents should read as "Hello world from C#"

The WriteAllText() method writes a sequence of ASCII charaters characters to text file.