?
Solved

C# StreamWriter and Encoding Problem when creating a file

Posted on 2007-03-22
2
Medium Priority
?
560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 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