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

Gol*_*old 4 c#

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

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

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

And*_*ker 11

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

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

var img = Properties.Resources.GifName;
Run Code Online (Sandbox Code Playgroud)

对于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.<filename>, 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
Run Code Online (Sandbox Code Playgroud)

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


Tom*_*röm 7

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

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