Archive for Ottobre 11th, 2007

Making Label show multiline text

Ottobre 11th, 2007

Description: When data is collected via a textbox and line breaks are provided in the input, this is a way to show those breaks in a label.

Link: http://www.codekeep.net/snippets/406f2f4c-bb8b-47a9-957b-3d5f40079702.aspx

 public string WrappableText(string source)
    {
        string nwln = Environment.NewLine;
        return "<p>" +
        source.Replace(nwln + nwln, "</p><p>")
        .Replace(nwln, "<br />") + "</p>";
    } 

Using Stream Reader And Stream Writer

Ottobre 11th, 2007

Description: Using Stream Reader And Stream Writer

Link: http://www.codekeep.net/snippets/801e1ebb-db64-492b-a543-dfabf0b176c6.aspx

using System.IO;
-----------------

// *** Write to file ***

// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);

// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);

// Write a string to the file
sw.Write("Hello file system world!");

// Close StreamWriter
sw.Close();

// Close file
file.Close();

// *** Read from file ***

// Specify file, instructions, and privelegdes
file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);

// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);

// Read contents of file into a string
string s = sr.ReadToEnd();

// Close StreamReader
sr.Close();

// Close file
file.Close();