如何隐藏在我的项目的GIF或MP3文件? [英] How to Hide gif or mp3 files in my project?

查看:78
本文介绍了如何隐藏在我的项目的GIF或MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些GIF和MP3文件

I have a C# project with some gif and mp3 files

如何将我的项目中结合这些文件?

how I can combine those files within my project?

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

(I don't want them to be visible to users)

推荐答案

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

You need to include them in the project as resources, and then access them later by reading from the DLL.

有关GIF文件你可以简单地把它们的资源(在项目 - >属性对话框),并然后通过

For gif files you can simply drop them on the resources (in the project->properties dialog) and then access them via

var img = Properties.Resources.GifName;

有关您可能需要使用嵌入式的资源,然后读出来作为流MP3文件。要做到这一点,拖动项目致力于为这些类型的文件在项目中的文件夹。右键点击资源管理器中的文件并显示属性面板,并设置了建设行动,以嵌入的资源。

For mp3 files you will probably need to use embedded resources, and then read them out as a stream. To do this, drag the item to a folder in your project dedicated to these types of files. Right click on the file in the explorer and show the properties pane, and set the "build action" to "embedded resource".

您可以再使用的代码是这样的(从VB,对不起,未经测试的翻译),以东西拿回来了作为一个流。这取决于你将流转换成能您的播放器可以处理。

You can then use code something like this (untested translation from vb, sorry), to get the thing back out as a stream. It's up to you to transform the stream into something your player can handle.

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

下面是几个环节,让你开始

Here are a few links to get you started

  • Microsoft Guide
  • Reading Resources from the DLL

这篇关于如何隐藏在我的项目的GIF或MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆