是否可以在类中包含文件
有没有办法在类中包含文件内容,不仅仅是文本文件,还有二进制文件,例如Char Array
,或String 或地球上的任何东西,所以在构造函数中,我的类应该将该内容写入指定文件中的文件。
该文件可以是 .exe 、 .pdf 、 .xls 、 .xml 等...
Is there a way to include a File Content inside the Class ,not just Text Files but also Binary Files like a Char Array
,or String
or whatever in Earth it could be ,so in Constructor my Class Should write That content into a File in a specified file .
This File can be an .exe , .pdf , .xls , .xml etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太确定你所说的包含“类中的文件内容”是什么意思,但从我猜测你的意思(将整个文件存储在类中?)你可能想看看 Base64 编码,这是一种编码方案它可以让您将任何二进制数据转换为 ASCII 字符串。
要存储二进制数据,您可以使用
Convert.ToBase64String
将任何byte[]
转换为 Base64 编码字符串,并将其存储在您的类中。要取回它,请使用Convert.FromBase64String
获取byte[]
。I'm not exactly sure what you mean by include "a File Content inside the Class" but from what I'm guessing you mean (storing entire files inside a class?) you may want to look at Base64 encoding which is an encoding scheme that will let you convert any binary data into a ASCII string.
To store your binary data you can convert any
byte[]
into a Base64 encoded string usingConvert.ToBase64String
and store that in your class. To get it back useConvert.FromBase64String
to get abyte[]
.就像cbley一样,我也不太确定你的意思,但你可能意味着在程序集中嵌入一个文件?
当您运行应用程序时,您可以将文件二进制文件提取到文件系统,如下所示。
Like cbley, I'm not exactly sure what you mean either, but you could mean embedding a file in your assembly?
When you run your application, you can extract the file binary to the file system like this.
看一下
File.ReadAllBytes()
方法。Have a look at
File.ReadAllBytes()
method.这是可能的,只需将文件内容存储为字节数组即可。编写一个转换器,将指定文件的字节以您需要的格式保存到文本文件中(因此它代表一个数组)。然后将其剪切并粘贴到您的源代码中。
That would be possible, just store the file content as byte array. Write a converter which saves the bytes of a specified files into a text file in the format you need (so it represents an array). Then cut and paste this into your source code.