3

When saving a text file client gets 'Â' character infront of every currency sign.

I tried the following but still getting strange characters

using (StreamWriter sr = new StreamWriter(path, false, System.Text.Encoding.UTF8)) { sr.Write(text); } using (StreamWriter sr = File.CreateText(path)) { sr.Write(text); } 

any suggestions on how to save a file without any encoding?

7
  • 4
    You can't save a file without any encoding, the default encoding depends on the local system (msdn.microsoft.com/en-us/library/…). What kind of currency symbols are you talking about? only $ or are there others? Commented Nov 14, 2011 at 11:43
  • The problem is almost certainly that the client is using the wrong encoding. You should find out what encoding they are expecting / can handle successfully and use that. It may be helpful to provide a BOM Commented Nov 14, 2011 at 11:44
  • How are you opening this file? It seems more likely that the flaw is there than here. Using anything other that UTF-8 should be strongly discouraged if at all possible. Alas, TARDISs don't exist or we could sort it out for once and for all ;) Commented Nov 14, 2011 at 11:55
  • I am not opening this file client is.I am saving this file Commented Nov 14, 2011 at 12:20
  • @Justm they seem they want Ansi Commented Nov 14, 2011 at 12:20

3 Answers 3

15

any suggestions on how to save a file without any encoding?

That question is meaningless; the "encoding" is the process of translating string characters to bytes to store/transfer (for example on disk). Any text content has to have an encoding. If you have saved it as UTF8, then the person reading it must also be reading it as UTF8. You could try including a BOM:

... new StreamWriter(path, false, new UTF8Encoding(true)) 

Alternatively, find out what encoding/code-page YOUR MACHINE is configured to use by default, and use that (it is Encoding.Default); for example:

... new StreamWriter(path, false, Encoding.Default) 

If your user indicates that they need it in code-page 874 (aka "Thai"), then you would use:

... new StreamWriter(path, false, Encoding.GetEncoding(874)) 
Sign up to request clarification or add additional context in comments.

5 Comments

@MarcGravell .Thanks for your reply.When client saves it to Ansi it does not get problem .
@user231465 there is no single ansi. Either you mean ascii (7-bit only), or you mean some specific codepage, and you need to know which code-page it is. The current system-default code-page is, already noted, Encoding.Default. However, note that in a server/client scernaio it is likely that the server's default code-page and the client's default code-page are different.
@user231465 if the user indicates that they are using, for example, code-page 874 ("Thai"), then you would use (as the final parameter) Encoding.GetEncoding(874)
@MarcGravell thanks for your suggestion all sorted .Agreed with client and we both use same encoding now
How to find out? How to change? Thats the real question I guess isnt it?
8

StreamWriter with a stream as input would default to UTF-8 according to the documentation.

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array.

2 Comments

this seems to be the only answer that actually answers the question
@Andy this seems to be the only answer that answers the question in the title. The OP asks something different in the body of the question.
3

This depends on your computer's local settings. If you want to be sure everything is saved as you intended, you should specify the encoding yourself.

See: http://msdn.microsoft.com/en-us/library/system.text.encoding.default.aspx

1 Comment

The default encoding is the "encoding for the operating system's current ANSI code page."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.