ascii file viewer

  • Thread starter Thread starter Michele
  • Start date Start date
M

Michele

Hi,

i need an ascii file viewer.
How can i find the source or documentation for it?
Naturally in c#

Thanks and sorry for my bad english
 
ASCII file viewer:

Small files: StreamReader.ReadAll() and display

Larger files need buffering before displaying, but the method is to loop,
either line by line (preferable in many cases) or chunked.

The code for basic viewing should be about 10 lines of actual written code.
Be sure to close the reader after you are done. :-)

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
Small files: StreamReader.ReadAll() and display

thanks but i have try it but the result is bad.
I want read the ascii file like i read it if i open it with Notepad.
 
In what way is the result bad?

Notepad reads ANSI or Unicode files, not ASCII files.
 
Use the System.Text.ASCIIEncoding class to translate from ASCII to Unicode.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Hello Michele,

here are two examples. Place a RichTextControl on a form and try this code:

// line numbers formatter for file viewer
const string OpenBracketLineNumber = "[";
const string CloseBracketLineNumber = "]";
const string Space = " ";

private void DisplayTextFile() {
// change mouse cursor to hourglass
Cursor.Current = Cursors.WaitCursor;

// clear displayed text
richTxtFileContents.Clear();

//selected filename = lstBoxSelectedFileNames.SelectedItem.ToString()
using (TextReader reader = new
StreamReader(lstBoxSelectedFileNames.SelectedItem.ToString())) {
// current line number
int lineNumber = 0;

// first line
string line = reader.ReadLine() + Environment.NewLine;
StringBuilder lines = new StringBuilder(OpenBracketLineNumber +
++lineNumber + CloseBracketLineNumber + Space + line);

while ((line = reader.ReadLine()) != null) {
lines.Append(OpenBracketLineNumber +lineNumber + CloseBracketLineNumber +
Space + line + Environment.NewLine);
}

// add text to display
richTxtFileContents.Text = lines.ToString();

}
// change mouse cursor to default
Cursor.Current = Cursors.Default;
}

or this:


private void DisplayTextFile() {

// change mouse cursor to hourglass
Cursor.Current = Cursors.WaitCursor;

// clear displayed text
richTxtFileContents.Clear();

FileInfo fi = new
FileInfo(lstBoxSelectedFileNames.SelectedItem.ToString());
FileStream fs = fi.OpenRead();
byte[] file = new byte[fs.Length];
fs.Read(file, 0, (int)fs.Length);
// add text to display
richTxtFileContents.Text = Encoding.ASCII.GetString(file);

// change mouse cursor to default
Cursor.Current = Cursors.Default;

}
 
Hello Michele,

I used this code for to view hundreds of ASCII files.

Please answert the following questions:

What exactly don't work?
Can you specify it more in detail?
Are you sure you use a ASCII file?

-
Alexander Todorovic
 
Back
Top