views:

263

answers:

1

I need to write the following string to a txt a File:

SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX ARTURO,584

I when I use:

using  (System.IO.StreamWriter sw = new System.IO.StreamWriter(@fileSobrantes, true)) {
       sw.WriteLine("SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ   FELIX ARTURO,584");
}

I get this in the file

SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX ARTURO,584

I try with the Encoding parameters in ASCII, UNICODE and ALL UTF and does not work.

System.IO.StreamWriter(@fileSobrantes, true,Encoding.UTF32 ))
+1  A: 

You can't easily (and accurately) represent what you get in the file without giving a hex dump. What are you trying to use to read the file? My guess is that if you try Encoding.Default that will work for you, but it's hard to say for sure without knowing what you're trying to use to read it.

The other alternative is that your source string is incorrect. If you've really got it as a string literal in your source code, are you sure you've got Visual Studio set up to interpret it correctly?

See my unicode debugging page for suggested techniques.

EDIT: By the way, why are you prefixing fileSobrantes with @? For identifiers you only need to do that if they're keywords. You may be getting confused with verbatim string literals - but this isn't a string literal, it's a variable name.

Jon Skeet
The @fileSobrantes is because I had something like this "c:\tmp\files" and when I did the refactoring I forgot to delete the @. Thanks
Jedi Master Spooky
No problem - glad the answer helped. What was wrong in the end?
Jon Skeet
I use the Encoding.Default and now I can open in Excel, Word and WordPad correctly. If I open en notepad or UltraEdit I get the wrong characters. But I need it to open in Excel. Thanks
Jedi Master Spooky
Okay. I'd suggest writing in UTF-8 and working out how to tell Excel that it really is using UTF-8, but if what you've got works for you, then that's fine :)
Jon Skeet