如何在我的项目中隐藏gif或mp3文件?

我有一个带有一些gif和mp3文件的C#项目

我如何在项目中组合这些文件?

(我不希望它们对用户可见)

您需要将它们作为资源包含在项目中,然后通过读取DLL来访问它们。

对于gif文件,您只需将它们放在资源上(在项目 – >属性对话框中),然后通过它们访问它们

var img = Properties.Resources.GifName; 

对于mp3文件,您可能需要使用嵌入式资源,然后将其作为流读出。 为此,请将项目拖动到项目中专用于这些类型文件的文件夹中。 右键单击资源管理器中的文件并显示属性窗格,并将“构建操作”设置为“嵌入资源”。

然后,您可以使用类似这样的代码(来自vb的未经测试的翻译,抱歉),以便将该内容作为流返回。 您可以将流转换为玩家可以处理的内容。

 using System.Linq; // from System.Core. otherwise just translate linq to for-each using System.IO; public Stream GetStream(string fileName) { // assume we want a resource from the same that called us var ass = Assembly.GetCallingAssembly(); var fullName = GetResourceName(fileName, ass); // ^^ should = MyCorp.FunnyApplication.Mp3Files., or similar return ass.GetManifestResourceStream(fullName); } // looks up a fully qualified resource name from just the file name. this is // so you don't have to worry about any namespace issues/folder depth, etc. public static string GetResourceName(string fileName, Assembly ass) { var names = ass.GetManifestResourceNames().Where(n => n.EndsWith(fileName)).ToArray(); if (names.Count() > 1) throw new Exception("Multiple matches found."); return names[0]; } var mp3Stream = GetStream("startup-sound.mp3"); var mp3 = new MyMp3Class(mp3stream); // some player-related class that uses the stream 

以下是一些可以帮助您入门的链接

  • Microsoft指南
  • 从DLL中读取资源

将它们添加为嵌入式资源,它们将被编译到dll中。

首先将它们添加到项目中,只需将它们拖到解决方案资源管理器中,然后转到其属性以将其构建操作更改为嵌入式资源。

将它们包含在资源文件(resx)中。

您应该将它们分配为资源。 如果在VS中打开“属性”面板,请确保将构建操作设置为“资源”,并将“复制到输出目录”设置为“不复制”。 您还可以在资源选项卡中的项目属性中管理资源。