Solved

C# StreamWriter and Encoding Problem when creating a file

Posted on 2007-03-22
2
546 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now