对我来说,在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文件中
发布评论