Solved

C# StreamWriter and Encoding Problem when creating a file

Posted on 2007-03-22
2
554 Views
Last Modified: 2013-11-26
I am trying to store an access database within a property in my class (as a string variable - is this correct? probably not!)

I can read the ms access db into my class using a streamreader but when I try to save the datbase back using a stream writer i get the following error:
------------------
Found a high surrogate char without a following low surrogate at index: 716.  The input may not be in this encoding, or may not contain valid Unicode (UTF-16) characters. Parameter name: chars
------------------

Here is my code for reading and writing the db:
Reading the database into my class:
System.IO.FileStream file = new System.IO.FileStream(dlgFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.StreamReader reader = new System.IO.StreamReader(file);
MyClass myClass = (MyClass)tvwPasswords.SelectedNode.Tag;
myClass .Document = reader.ReadToEnd();

Writing back to a file:
System.IO.FileStream file = new System.IO.FileStream(dlgFile.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter writer = new System.IO.StreamWriter(file);
writer.Write(myClass.strDocument);
writer.Close();
file.Close();

Any Solutions Welcome!
0
Comment
Question by:benwilliamson
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 18784762
instead of storing into a string i would prefer byte stream.. excatly why are u storing this in a string
0
 

Author Comment

by:benwilliamson
ID: 18784943
Hi - thanks, yes you are right, a byte array did the trick.  I used a binaryreader/writer instead too and it seems to work - cheers.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question