Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# StreamWriter and Encoding Problem when creating a file

Posted on 2007-03-22
2
Medium Priority
?
565 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

610 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