Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Reading text file that contains special characters

cohoonma (MIS) (OP)
2 Mar 06 16:16

I am trying to create a simple C# windows app that contains a couple of file list boxes and a text box. When the entry in the file list box is selected, I want the contents to display in the text box. Simple enough. My problem is that I am trying to display Microsoft SMS MIF files from SMS clients that had an issue trying to insert their MIF file into the SMS database. When I look at the file using notepad, the file contains wierd characters like these:  Õ throughout the file. Note: these characters are supposed to be there by design as SMS creates the files that way.

When I try to dump the file into a text box, all I get is the first wierd character. I am guessing the text reader is interpreting the character as an end of file. Does anyone know how I can just dump the whole file into the text box, regardles off what text it contains? My code is listed below:


// Determine if the file exists before loading.
if (System.IO.File.Exists(file))
  // Open the file and use a TextReader to read the contents into the TextBox.
  System.IO.FileInfo myFile = new System.IO.FileInfo(file);
  System.IO.TextReader myData = myFile.OpenText();;

  txtInventoryFile.Text = myData.ReadToEnd().ToString();
// Exception is thrown by the OpenText method of the FileInfo class.
   MessageBox.Show("The file you specified does not exist.");
// Exception is thrown by the ReadToEnd method of the TextReader class.
   MessageBox.Show("There was a problem loading the file into the TextBox. Ensure that the file is a valid text file.");
earthandfire (Programmer)
2 Mar 06 16:19
Temporarily give the file a non text file extension eg .bin and open it in VS.

You should see a hex dump of the file and from that sdhould be able to identify the offending characters.

Hope this helps.


cohoonma (MIS) (OP)
2 Mar 06 16:24

thanks, however, I really don't care what the characters are. I am just scanning the file to find a computer name that is contained within it. I use that infor for other items related to SMS Administration. Just a complete dump of the file is all that I am looking to do regardless of whether the file has any wierd characters or not. Do you think that is possible?

earthandfire (Programmer)
2 Mar 06 16:28
Sorry, I didn't read the last sentence of you original post properly.


chiph (Programmer)
2 Mar 06 16:32
If you know the encoding for the file, you can read the file as a series of bytes, strip out the offending bytes, and then call one of the Encoding family of classes (ASCIIEncoding, Utf8Encoding, EBCDICEncoding, etc) to convert them to a human-readable string with the GetString() method.

Chip H.

If you want to get the best response to a question, please read FAQ222-2244 first

cohoonma (MIS) (OP)
3 Mar 06 9:24

Not being a full-time programmer, I tried researching your idea of encoding.

I grabbed a couple of examples from MSDN and tried reading in the file and seeing what happens.

I unfortunately got the same results. My guess is I need to somehow figure out what encoding, if any, SMS is using to write the inventory file I am trying to dump into the text box. Is there someway to determine the encoding?

chiph (Programmer)
3 Mar 06 9:53
You can make some guesses, but generally unless they tell you, it's difficult to determine.  Given that it's a Windows program creating it, my guesses in decreasing order of probability are:

1. Win-1251 (Latin-1)
2. Whatever the default locale for the operating system is.
3. UTF-16
4. UTF-8

Chip H.

If you want to get the best response to a question, please read FAQ222-2244 first

cohoonma (MIS) (OP)
3 Mar 06 11:54
Well, I have come up with a solution.
I took the approach of not trying to dump the whole file in to the textbox. I instead stumbled upon using the IndexOf() function to find the start of the computer name, then the Substring() function for the correct length. I guess I thought it would have been easier to just dump the whole file rather than pick out the computer name. Turned out to be the exact opposite though. the index and substring made it easy.

thanks for all of your time and suggestions.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close