简体   繁体   English

如何在C#中将数据保存到文本文件以外的其他文件

[英]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.

相关问题 如何将List &lt;&gt;数组中的数据保存到文本文件(C#)? - How save data in List<> array to a text file (C#)? c#如何将字典保存到文本文件 - c# how to save dictionary to a text file 如何使用UTF-8以外的代码页在C#中写出文本文件? - How do I write out a text file in C# with a code page other than UTF-8? 如何将 stream 的 Json 数据保存到 C# Windows Forms 应用程序中的文本文件? - How can I save a stream of Json data to a text file in C# Windows Forms app? C#Datagridview-如何组织和设置某些列和单元格值以及将数据保存在文本文件中 - C# Datagridview - How to organize and set certain column and cell values and save the data in a text file 如何使用 Xamarin 表单和 C# 将网站中的 Html 数据保存到文本文件 - How to Save Html data from a website to a text file using Xamarin forms and C# 如何使用C#在Unity中使用分隔行将文本保存到文件 - How to save text to file with the separated lines in Unity with c# 如何从列表保存内容 <string> 到C#中的文本文件? - How to save content from List<string> to a text file in C#? 如何使用c#在文本文件中保存带有索引的列表项 - How to save items of list with their index in text file using c# CodeGo.net&gt;如何将对象列表保存到文本文件 - c# - How to save List of objects into a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM