Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

c# - How to Hide gif or mp3 files in my project?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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;

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".

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...