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

发布于 2024-09-04 04:15:16 字数 347 浏览 3 评论 0原文

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

代码片段

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

对我不起作用!

在“WriteAllBytes”上它说: “无法从‘System.Drawing.Bitmap’转换为‘byte[]’” 对于图像和 “无法从‘字符串’转换为‘字节[]’” 对于文本文件。

帮助!

更新:下面解决了。

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.

Snippet

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.

Help!

UPDATE: Solved below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吹泡泡o 2024-09-11 04:15:16

将所需的文件添加到解决方案中,然后将其Build Action 属性设置为Embedded Resource。这会将文件嵌入到您的 exe 中。 (msdn

然后你只需要写执行 exe 时将文件写入磁盘的代码。

例如:

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

resource.bmp 替换为您的文件名。

附录

如果您将该文件保存在解决方案的子文件夹中,则需要将该子文件夹设为 resource.bmp 路径的一部分。例如:

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

此外,您可能需要将复制到输出目录属性设置为始终复制如果较新则复制

Add the files you want to your solution and then set their Build Action property to Embedded Resource. This will embed the file into your exe. (msdn)

Then you just need to write the code to write the file out to disk when the exe is executed.

Something like:

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

Replace resource.bmp with your file name.

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. 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.

无悔心 2024-09-11 04:15:16

我假设您通过“项目属性”窗口添加了文件。这不允许您添加任意文件,但它支持文本文件、位图等。

对于嵌入的文本文件,请使用

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

对于图像,请使用

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

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.

For an embedded TextFile, use

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

For an Image, use

  Properties.Resources.Image1.Save(@"C:\MyFile.bin");
一笔一画续写前缘 2024-09-11 04:15:16

可以将二进制文件嵌入到.resx 文件中。将它们放在文件部分中(看起来您使用了图像部分)。如果您的 .resx 文件生成 .Designer.cs 文件,则它应该可以作为字节数组进行访问。

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

You can embed binary files in a .resx file. 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.

File.WriteAllBytes(@"C:\foobar.exe", Properties.Resources.foobar);
Smile简单爱 2024-09-11 04:15:16

将文件添加到项目资源并将其“构建操作”设置为“嵌入资源”。

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

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

根据本文,不知道它有多深是正确的:http://www.yoda.arachsys.com/csharp/readbinary.html
但它有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文