如何使用C#创建ANSI文件 [英] How to Create ANSI file using C#

查看:941
本文介绍了如何使用C#创建ANSI文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有 使用C#创建一个ANSI文件。


我真正的问题是我有一个ANSI文件 来自Linux API的enconding格式(我无法访问这台机器,我只是可以使用API​​)


我需要创建此文件的副本并更改内容这个新文件。



问题是,我做了这个文件的副本,当我尝试清除内容时,它会保留一些内容该文件写入新内容,它会自动更改此新文件的encondig。


这些是我的步骤:


1)复制ANSI文件(ok)


File.Copy("source.txt","new_file.txt");


2)删除此新文件的内容


File.WriteAllText(" new_file.txt",string.Empty,Encoding.Default);


此时它已经改变了这个新文件的含义



<请问,有人对这个问题有所了解吗?



问候,



William




解决方案

.NET的默认编码是Unicode。保存文件时,请使用
编码。如果您将自己限制为ASCII字符,则Ascii
而不是Encoding.Default。否则请使用Utf8,看看是否能正确返回Linux。


Hello Everyone,

I have  to create an ANSI file using C#.

My real problem is I have a File in ANSI  enconding format comming from a Linux API(I do not have access to this machine, I just may use the API)

and I need to create a copy of this file and change the content of this new file.

The problem is, I do a copy of this file and it keeps the enconding howerver when I try to clear the content of this file to write the new content it changes automatically the encondig of this new file.

These are my steps:

1) Make Copy the ANSI FIle (ok)

File.Copy("source.txt", "new_file.txt");

2)Erase the content of this new file

File.WriteAllText("new_file.txt", string.Empty, Encoding.Default);

In this point it has changed the enconding of this new file

Please, someone has an idea about this issue?

Regards,

William

解决方案

The default encoding for .NET is Unicode. When saving the file use Encoding.Ascii instead of Encoding.Default if you are limiting yourself to ASCII characters. Otherwise use Utf8 instead and see if that gets back to Linux correctly.


这篇关于如何使用C#创建ANSI文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆