473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to change a streamwriter encoding?

Hi. I'm using that code:

If File.Exists(Ls_NombreFichero) = False Then
sw = File.CreateText(Ls_NombreFichero)
Else
sw = File.AppendText(Ls_NombreFichero)
End If

I need to change the encoding, as utf-8 is not the one I can use. How can I
change it? Encoding property is read only and I don't know how to use the
proper constructor.
--

Regards,

Diego F.


Nov 12 '07 #1
3 20233
On Nov 12, 6:39 am, "Diego F." <diego_f...@msn.comwrote:
Hi. I'm using that code:

If File.Exists(Ls_NombreFichero) = False Then
sw = File.CreateText(Ls_NombreFichero)
Else
sw = File.AppendText(Ls_NombreFichero)
End If

I need to change the encoding, as utf-8 is not the one I can use. How can I
change it? Encoding property is read only and I don't know how to use the
proper constructor.
--

Regards,

Diego F.
I'm a bit confused - the FileStream object, which is created by
CreateText and AppendText, does not have a Encoding property.

Thanks,

Seth Rowe

Nov 12 '07 #2
On Nov 12, 8:53 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Nov 12, 6:39 am, "Diego F." <diego_f...@msn.comwrote:
Hi. I'm using that code:
If File.Exists(Ls_NombreFichero) = False Then
sw = File.CreateText(Ls_NombreFichero)
Else
sw = File.AppendText(Ls_NombreFichero)
End If
I need to change the encoding, as utf-8 is not the one I can use. How can I
change it? Encoding property is read only and I don't know how to use the
proper constructor.
--
Regards,
Diego F.

I'm a bit confused - the FileStream object, which is created by
CreateText and AppendText, does not have a Encoding property.

Thanks,

Seth Rowe
Sorry - I confused myself, I was looking at the Create method and not
the CreateText method.

Perhaps you could create your StreamWriter from the FileStream
returned by File.Create()? Then you could set the encoding in the
constructor like so:

/////////////////////
sw = New StreamWriter(File.Create(Ls_NombreFichero),
System.Text.Encoding.UTF32)
/////////////////////

As far as the AppendText, I think you will need to read the file
contents, delete the old file, and then use the above method to
rewrite the file with the new encoding. There may be a way to change
the encoding of an existing file, but I'm not sure what it is.

Thanks,

Seth Rowe

Nov 12 '07 #3
Thank you. I could do it using that.

--

Regards,

Diego F.
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
On Nov 12, 8:53 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:
>On Nov 12, 6:39 am, "Diego F." <diego_f...@msn.comwrote:
Hi. I'm using that code:
If File.Exists(Ls_NombreFichero) = False Then
sw = File.CreateText(Ls_NombreFichero)
Else
sw = File.AppendText(Ls_NombreFichero)
End If
I need to change the encoding, as utf-8 is not the one I can use. How
can I
change it? Encoding property is read only and I don't know how to use
the
proper constructor.
--
Regards,
Diego F.

I'm a bit confused - the FileStream object, which is created by
CreateText and AppendText, does not have a Encoding property.

Thanks,

Seth Rowe

Sorry - I confused myself, I was looking at the Create method and not
the CreateText method.

Perhaps you could create your StreamWriter from the FileStream
returned by File.Create()? Then you could set the encoding in the
constructor like so:

/////////////////////
sw = New StreamWriter(File.Create(Ls_NombreFichero),
System.Text.Encoding.UTF32)
/////////////////////

As far as the AppendText, I think you will need to read the file
contents, delete the old file, and then use the above method to
rewrite the file with the new encoding. There may be a way to change
the encoding of an existing file, but I'm not sure what it is.

Thanks,

Seth Rowe

Nov 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xmlguy | last post by:
This is part of the application that reads XML from file (that has UTF-8) into an XmlTextReader XmlTextReader myXmlReader = new XmlTextReader(args); It then instantiates an object/class...
4
by: Jaroslav Jakes | last post by:
Hi, please help. Sounds so simple. We receive textfiles (customer orders) as e-mail attachment. These textfiles contain a simple structure of orders, like: custno, itemno, qty, text Since...
1
by: Alexander Cohen | last post by:
How would i go about changing a databases encoding? Is this at all possible? There does not seem to be much i can with ALTER DATABASE except change its name! -- Alexander Cohen...
0
by: Brad Wood | last post by:
I have a web service that returns an xml doc as a string. I use a StringWriter with my XmlTextWriter and the encoding attribute for the document becomes utf-16. If I save the result of the web...
6
by: 6kjfsyg02 | last post by:
I have written a client to a web service. I use ASP.NET 1.1 for the client. It worked until I tried to send accented characters. Then the service answered that my signature is not valid. I was...
2
by: lprisr | last post by:
Hi, I have double byte characters in the content that I am returning using Web Services. However, the encoding in the xml file returned by Web Services is utf-8 and I am unable to read the...
4
by: perspolis | last post by:
Hi all how can I change default encoding for saving C# files?? I want to set it always to Unicode -CodePage1200. thanks.
4
by: aradhanathawait | last post by:
Hi all, Can I change the character encoding of an existing database. I have to store Unicode data in the database. Please help. Regards, Aradhana
3
by: hamidfirooze | last post by:
Hi I want to read a txt file and write its contents into a textbox. The problem is that the content of txt file is not in English and I have to change the encoding. So how could I change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.