C# 如何在 EXE 中存储文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Store Files In An EXE
提问by S3THST4
Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do File.Copy(file, filedir);
好的,所以我正在用 C# 编写我自己的安装程序,我想做的是将文件放入 .exe 中,这样我就可以执行 File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
或者,如果这是不可能的,是否有另一种方法可以做我正在尝试做的事情?
采纳答案by Shawn Miller
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
我不会编写自己的安装程序,但如果您真的想将文件嵌入到程序集中,则可以使用强类型资源。在项目的属性对话框中打开“资源”选项卡,然后添加您的文件。然后,您将能够使用以下方法获取文件:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
然后,您将能够使用以下方法将嵌入的资源写入磁盘:
System.IO.File.WriteAllBytes(@"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
回答by Ray Booysen
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
我在这里猜测,但是如果您尝试在编译前将资源存储在应用程序中,您可以在项目资源管理器中,右键单击要添加的文件,选择属性并将类型更改为嵌入式资源。
You can then access the embedded resources later by using the instructions from this KB: http://support.microsoft.com/kb/319292
然后,您可以稍后使用此知识库中的说明访问嵌入的资源:http: //support.microsoft.com/kb/319292
回答by Erik Funkenbusch
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
老实说,我建议您不要创建自己的安装程序。创建安装程序有很多问题。即使是大型安装程序制造商也不再制作自己的实际安装程序,他们只是创建自定义 MSI 包。
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
使用 Mirosoft 安装程序 (MSI)。这是正确的做法。为它制作您自己的自定义前端,但不要重新创建已经存在的非常复杂的轮子。
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
更新:如果您只是为了学习而这样做,那么我会回避将其视为“安装程序”。有一天,您可能很想进行“研究”并使用它,坦率地说,这就是当新版本的 Windows 出现时我们最终会遇到这么多问题的原因。人们用无效的假设创建自己的轮子。
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
您真正想做的是所谓的“打包”,您确实必须非常熟悉 Executable PE 格式,因为您正在谈论更改磁盘上 PE 映像的结构。
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
在某种程度上,您可以通过将文件放入资源中来模拟它,但这并不是安装程序或自解压程序所做的。
Here's a link to Self-Extractortutorial, but it's not in C#.
这是自解压器教程的链接,但它不在 C# 中。
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
我对 .NET PE 要求的了解不够,不知道您是否可以使用托管代码可执行文件执行此操作。
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demowritten in C#.
UPDATE2:这可能是您要查找的更多内容,它将文件嵌入到资源中,但正如我所说,这并不是专业安装人员或自解压程序的真正方式。我认为您可以作为资源嵌入的内容存在各种限制。但这里类似于用 C# 编写的自解压器演示。
回答by Shawn Miller
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.
如果您只是想在单个文件存储中存储多个文件(并从那里提取文件、交互等),您可能还想查看 NFileStorage,一个.net 文件存储。用 100% .NET C# 编写,包含所有源代码。它还带有一个命令行解释器,允许从命令行进行交互。