简体   繁体   English

C#以编程方式嵌入文件

[英]C# Embed File Programmatically

I have a requirement where I want a user to select a file and it will embed it into my application 我有一个要求用户选择文件并将其嵌入到我的应用程序中的要求

I know how to embed a resource manually, however I don't know if there is a way to do it programmatically when the user selects a file. 我知道如何手动嵌入资源,但是当用户选择文件时,我不知道是否可以通过编程方式来实现。

If you mean actually embed data into your 'application.exe', this is not possible without a second executable to perform the operation. 如果您的意思是实际上将数据嵌入到“ application.exe”中,那么没有第二个可执行文件来执行此操作是不可能的。 The operating system puts a lock on a running executable, to prevent modifications like this. 操作系统将锁定正在运行的可执行文件,以防止进行此类修改。

If you 100% did want to store data in your executable, it would have to be right at the end of the file. 如果您100%确实希望将数据存储在可执行文件中,那么它必须位于文件末尾。 I've tried just dumping some random binary data into the end of an executable, and it seemed to run just fine, but there's probably consequences. 我试过只是将一些随机的二进制数据转储到可执行文件的末尾,它似乎运行得很好,但是可能会有后果。

What you should probably be doing instead is create and manage a folder on the user's machine -- maybe directly on the hard drive, on their user account, online, or in a folder they choose -- and store files there. 您可能应该做的是在用户计算机上创建和管理一个文件夹-可能直接在硬盘驱动器上,在他们的用户帐户上,在线或在他们选择的文件夹中-并在其中存储文件。 Then, load them when needed. 然后,在需要时加载它们。

This approach works for all resources. 这种方法适用于所有资源。 Please specify the resources you are wanting to 'embed' -- and why other approaches are unsuitable -- for better help. 请指定您想要“嵌入”的资源-以及为什么其他方法不合适-以寻求更好的帮助。

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

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