English 中文(简体)
修改编码
原标题:Forcing StreamWriter to change Encoding

我试图通过<代码>DialogRes和StringBuilder节省一个文件。 在提出案文后,我呼吁制定以下法典,以挽救档案:

    if (dr == DialogResult.OK)
    {

        StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);

        sw.Write(sb.ToString());
        sw.Close();
    }

I tried to add the second parameter to StreamWriter as Encoding.UTF8 but since the first argument is a string rather than a Stream, it does not compile it.

我怎么能将这种浮游转换为能够把第二个参数作为编码?

其原因是,在我文本I中,有些地方有<条码>微克/代码>,但在保存文件时,如<条码>1⁄4<条码>,<条码>正在改写!

增 编

最佳回答

StreamWriter sw = new StreamWriter(
    new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),
    Encoding.UTF8
);

If you want to append, use FileMode.Append instead.

您还应在<条码>上打上<>条码>。 范围:

using(
    var sw = new StreamWriter(
        new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),
        Encoding.UTF8
    )
)
{
    sw.Write(sb.ToString());
}

This will properly close and dispose the streams across all exception paths.

<><>UPDATE:

根据下文的JinThakur评论,

var sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8);

第二个参数是,<代码>StreamWriter,如果存在,是否应当附在档案中,而不是拖入。

问题回答

有一个档案名称、附录Mode、编码的构造者。

附有适当的<条码>。 它希望:

if (dr == DialogResult.OK)
{
    using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, 
           false, Encoding.UTF8))
    {
      sw.Write(sb.ToString());
      //sw.Close();
    }
}

http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx” rel=“nofollow” 页: 1

I said you ought to wrap your StreamWriter in a using too, i.e.

if (dr == DialogResult.OK)
{
    using(StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8)) {
        sw.Write(sb.ToString());
        sw.Close();
    }
}

尽管实事求是,这一胜利在这方面没有任何变化。 这有效地围绕法典进行了尝试/最终确定,这样,即使当时出现了一种例外情况,理商也会清理(请打sw.Dispose()。 (有些人会说,这也意味着你不再需要<代码>。) 贴,因为处置也会照顾到这一点,但我更愿意这样做。

setting UTF8 encoding working with Arabic font is the best thing I did:

 using (var sw = new StreamWriter(

 new FileStream(temporaryFilePath,    
               FileMode.Create,
               FileAccess.ReadWrite), 
               Encoding.UTF8))
            {
                sw.Write(sb.ToString());
            }
 )

The easiest way is to use the right constructor.

StreamWriter(String, Boolean, Encoding)

采用特定的编码和缺省缓冲规模,为特定档案采用一个新专级。 如果档案存在,该档案可以超出文字或附后。 如果档案不存在,该建筑商会创建新的档案。

C#

public StreamWriter (string path, bool append, System.Text.Encoding encoding);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签