[英]How to save data to other than text file in C#
我想知道如何将数据保存到文本文件以外的其他类型的文件中,例如,将其保存到未经任何编码的原始文件中,我的意思是只保存一个字节值,所以当您尝试在文本编辑器中对其进行编辑时,您将得到类似的内容: http://i.imgur.com/jVh2Ksk.png
You'll want to convert your string to a byte array. 您需要将字符串转换为字节数组。 See the following SO answer: String to raw byte array .
请参阅以下SO答案: 字符串到原始字节数组 。 You can then write the byte array to a file.
然后,您可以将字节数组写入文件。
In the System.Text.Encoding
namespace are some encodings with methods you can use to convert a string to a byte array easily. 在
System.Text.Encoding
命名空间中,包含一些方法的编码,您可以使用这些方法轻松地将字符串转换为字节数组。 Depending on the contents of your string you could use eg: 根据字符串的内容,可以使用例如:
var bytes = Encoding.ASCII.GetBytes(input);
var bytes = Encoding.UTF8.GetBytes(input);
var bytes = Encoding.UTF32.GetBytes(input);
Writing to a file can be easily archieved by: 通过以下方式可以很容易地归档文件:
File.WriteAllBytes(string path, byte[] bytes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.