将文件存储在C#EXE文件中

编程入门 行业动态 更新时间:2024-10-11 21:23:54
本文介绍了将文件存储在C#EXE文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对我来说,在EXE中存储一些文件以复制到所选位置实际上对我很有用. 我正在生成HTML和JS文件,需要复制一些CSS,JS和GIF.

It is actually useful for me to store some files in EXE to copy to selected location. I'm generating HTML and JS files and need to copy some CSS, JS and GIFs.

摘要

System.IO.File.WriteAllBytes(@"C:\ MyFile.bin",ProjectNamespace.Properties.Resources.MyFile);

System.IO.File.WriteAllBytes(@"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);

对我不起作用!

在"WriteAllBytes"上显示: 无法从'System.Drawing.Bitmap'转换为'byte []'" 用于图像和 无法从'字符串'转换为'字节[]'" 用于文本文件.

On "WriteAllBytes" it says: "cannot convert from 'System.Drawing.Bitmap' to 'byte[]'" for image and "cannot convert from 'string' to 'byte[]'" for text file.

帮助!

更新:在下面解决.

推荐答案

将文件添加到项目资源,并将其构建操作"设置为嵌入式资源".

Add files to project resources and set their "Build Action" as "Embedded Resource".

现在使用此代码段提取任何文件(文本或二进制文件):

Now extract any file (text or binary) using this snippet:

WriteResourceToFile("Project_Namespace.Resources.filename_as_in_resources.extension", "extractedfile.txt"); public static void WriteResourceToFile(string resourceName, string fileName) { int bufferSize = 4096; // set 4KB buffer byte[] buffer = new byte[bufferSize]; using (Stream input = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) using (Stream output = new FileStream(fileName, FileMode.Create)) { int byteCount = input.Read(buffer, 0, bufferSize); while (byteCount > 0) { output.Write(buffer, 0, byteCount); byteCount = input.Read(buffer, 0, bufferSize); } } }

根据本文,它不知道它的正确深度: www.yoda.arachsys/csharp/readbinary.html 但是可以.

Don't know how deep is it correct according to this article: www.yoda.arachsys/csharp/readbinary.html but it works.

更多推荐

将文件存储在C#EXE文件中

本文发布于:2023-11-13 06:00:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   EXE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!