1
 static void Main(string args[])
    {
        using (var streamWriter = new StreamWriter("D:\\path\\outputcsv"))
        {
          using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))
            `

This is my code to create a file named output file which is giving me a utf-8 file But my company need the output as utf-8 with bom file which I don't know how to create.

This is my file   1: https://i.stack.imgur.com/dBFFh.png

This is what to get   1: https://i.stack.imgur.com/yTIAp.png

I tried this

static void Main(string args[])
    {
        using (var streamWriter =new 
 StreamWriter("D:\\path\\outputcsv",false,UTF8Encoding(false)) 
        {
          using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))

but not working... Thanks in advance:)

I've edited the code like this

using (var streamWriter = new StreamWriter("D:\path\Output.csv", true,UTF8Encoding(true))) {

But still, it gives me the errors 1)cannot convert string to system.io.stream (on the file path) 2)cannot convert from bool to system.Text.Encoding 3)The name utf-8 encoding does not exist in the current context. The solution provided to a similar question is not working for me.

I could solve these errors by changing the code to

   using (var streamWriter = new StreamWriter("D:\\path\\Output.csv", true, System.Text.Encoding.UTF8))

but it doesn't show "UTF-8 with bom" in my csv file notepad

Shammu
  • 81
  • 7
  • 3
    "I tried this ... but not working" -- I assume you didn't *actually* try that, because what you wrote is a compiler error. You want `new UTF8Encoding(true)`, although you can also just use `Encoding.UTF8` (since `Encoding.UTF8` has the BOM enabled. Note that `StreamReader` by default [uses UTF-8 without BOM](https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs,72) – canton7 Nov 08 '22 at 15:09
  • In .NET Core `StreamWriter` uses UTF8 with a BOM by default, you don't need to do anything. In .NET Framework you need to pass `Encoding.UTF8`, otherwise the user's default encoding is used. You need `new UTF8Encoding(false)` to *disable* the BOM – Panagiotis Kanavos Nov 08 '22 at 15:12
  • @PanagiotisKanavos "*In .NET Core StreamWriter uses UTF8 with a BOM by default*" -- [not true](https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs,72). "*In .NET Framework you need to pass Encoding.UTF8, otherwise the user's default encoding is used*" -- [also not true](https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs,124) (I think you're thinking of `Encoding.Default`, which `StreamWriter` does not use) – canton7 Nov 08 '22 at 15:12
  • @canton7 you're right, I was thinking about UTF vs Encoding.Default. This means on .NET Core you get the desired behavior out of the box. So this question is about .NET Framework – Panagiotis Kanavos Nov 08 '22 at 15:17
  • @PanagiotisKanavos On both the default for `StreamWriter` is UTF-8 *without* BOM. OP wants UTP-8 with BOM – canton7 Nov 08 '22 at 15:19
  • Sure it does -- OP wants a BOM, and the things they've tried don't give them a BOM. They need `new UTF8Encoding(true)` or `Encoding.UTF8`, per my first comment – canton7 Nov 08 '22 at 15:20
  • @canton... Yes, it should be true. I understood. But this gives me following errors 1) "cannot convert from string to 'system.io.stream' "- on file path .. 2)The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?) on new utf-8 encoding(true) – Shammu Nov 08 '22 at 15:35
  • Please edit your question to include the code you're actually running. I doubt you're actually running `new utf-8 encoding(true)`.... Especially since I said to use `new UTF8Encoding(true)` – canton7 Nov 08 '22 at 15:41
  • Whoever voted to close this because it's not reproducible... I don't think you read the question, or at least you didn't try to reproduce it – canton7 Nov 08 '22 at 15:42
  • @ canton. I've edited the question. Please have a look – Shammu Nov 08 '22 at 16:39
  • I said `new UTF8Encoding(true)`, not `UTF8Encoding(true)`. Of course, you will need `using System.Text;` at the top of your file. You're also now inexplicably passing `true` as the `append` parameter, which means that StreamWriter will *append* to your file, rather than overwriting it. Which means that if the file didn't contain a BOM before, *appending* to the file won't add a BOM. – canton7 Nov 08 '22 at 17:07
  • It looks like you're flailing around and changing things without any understanding. This makes it very hard to help you, as it's impossible to predict what you've done – canton7 Nov 08 '22 at 17:08

0 Answers0