简体   繁体   English

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

[英]Store files in C# EXE file

It is actually useful for me to store some files in EXE to copy to selected location. 对我来说,在EXE中存储一些文件以复制到选定位置实际上很有用。 I'm generating HTML and JS files and need to copy some CSS, JS and GIFs. 我正在生成HTML和JS文件,需要复制一些CSS,JS和GIF。

Snippet 片段

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

doesn't work for me! 对我不起作用!

On "WriteAllBytes" it says: "cannot convert from 'System.Drawing.Bitmap' to 'byte[]'" for image and "cannot convert from 'string' to 'byte[]'" for text file. 在“WriteAllBytes”上,它说:“无法从'System.Drawing.Bitmap'转换为'byte []'”用于图像,“无法从'string'转换为'byte []'”用于文本文件。

Help! 救命!

UPDATE: Solved below. 更新:解决方案如下。

Add the files you want to your solution and then set their Build Action property to Embedded Resource . 将所需文件添加到解决方案中,然后将其“ Build Action属性设置为“ Embedded Resource This will embed the file into your exe. 这会将文件嵌入到您的exe中。 ( msdn ) msdn

Then you just need to write the code to write the file out to disk when the exe is executed. 然后你只需要编写代码,在执行exe时将文件写入磁盘。

Something like: 就像是:

File.Copy("resource.bmp", @"C:\MyFile.bin");

Replace resource.bmp with your file name. resource.bmp替换为您的文件名。

Addendum: 附录:

If you keep the file in a sub-folder in your solution you need to make the sub-folder part of the path to resource.bmp . 如果将文件保存在解决方案的子文件夹中,则需要将子文件夹作为resource.bmp路径的一部分。 Eg: 例如:

File.Copy(@"NewFolder1\resource.bmp", @"C:\MyFile.bin");

Also, you may need to set the Copy To Output Directory property to Copy Always or Copy If Newer . 此外,您可能需要将“ Copy To Output Directory属性设置为Copy Always或“ Copy If Newer

I assume you added the files through the Project Properties window. 我假设您通过“项目属性”窗口添加了文件。 That does not allow you to add an arbitrary file but it does support TextFiles, Bitmaps and so on. 这不允许您添加任意文件,但它支持TextFiles,Bitmaps等。

For an embedded TextFile, use 对于嵌入的TextFile,请使用

  File.WriteAllText(@"C:\MyFile.bin", Properties.Resources.TextFile1);

For an Image, use 对于图像,请使用

  Properties.Resources.Image1.Save(@"C:\MyFile.bin");

You can embed binary files in a .resx file. 可以将二进制文件嵌入.resx文件中。 Put them in the Files section (it looks like you used the Images section instead). 将它们放在“ 文件”部分(看起来您使用的是“ 图像”部分)。 It should be accessible as an array of bytes if your .resx file generates a .Designer.cs file. 如果您的.resx文件生成.Designer.cs文件,它应该可以作为字节数组访问。

File.WriteAllBytes(@"C:\foobar.exe", Properties.Resources.foobar);

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);
        }
    }
}

Don't know how deep is it correct according to this article: http://www.yoda.arachsys.com/csharp/readbinary.html but it works. 根据这篇文章,不知道它有多深: http//www.yoda.arachsys.com/csharp/readbinary.html但是它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM