Archive

Archive for March, 2008

Visual C# Note: Embed Wave File in .Net Application

March 20th, 2008 No comments

When writing small-scale applications, I think it’s good practice to embed resources, such as images, sound waves, and icons, in the application. This way you don’t need to worry about remembering to add separate files when you distribute your applications — a single .exe file is all you need.

I just finished a small project (“Tetris”) that utilized embedded wave files. Using an embedded wave file involves two steps. The first step, off course, is to “embed” the wave file in the application. This is actually is very easy. With you project open in Visual Studio 2005, simply drag your wave file and drop it in your project in the “Solution Explorer”. Then set “Build Action” to “Embedded Resource”, see Figure 1. When you build the project next time, the wave file will be embedded in the application.

Wave File Properties
Figure 1. To embed a file, set “Build Action” to “Embedded Resource”.

The second step is to access the wave file when you need to play it. You need to create a stream and associate your embedded wave file to the stream. Then you can use a SoundPlayer object to read from this stream and play the embedded wave file. Below are the codes to play my embedded wave file. Note that when you access the embedded wave file, you have to use format “Namespace.WaveFileName”. In my case, the namespace is “Tetris” and the wave file name is “Tetris.wav”. Therefore I have to use “Tetris.Tetris.wav” to access it.


using System.media;
using System.Reflection;
// Open the embedded wave file using a SoundPlayer object
Assembly asm = Assembly.GetExecutingAssembly();
Stream sm = asm.GetManifestResourceStream("Tetris.Tetris.wav");
// Play embedded wave file
sPlayer = new SoundPlayer(sm);
sPlayer.Play();

Categories: C# Tags:

Visual C# Note: Set Icon of Compiled .exe File

March 20th, 2008 No comments

The default icon for compiled .exe files in Visual Studio 2005 is the boring blank window icon. However, you can create a cool-looking icon and assign it to your compiled .exe file. Here is how to do it:

  1. In the “Solution Explorer”, right click your project name and select “Properties”. The “Properties” window will open.
  2. In the “Properties” window, select the “Application” tab. Click the “…” button on the right of the “Icon” dropdown box to select the icon file you created for your .exe file.
  3. Build the project then you will have a .exe file with your nice-looking icon -:), like “Tetris.exe” in Figure 1.
Exe File with Nice Icon
Figure 1. “Tetris.exe” has a customized icon while “Tetris.vshost.exe” has the default icon.
Categories: C# Tags:

Visual C# Note: Definition of Stream

March 19th, 2008 No comments

Found on About.com:

Definition: A stream is an important concept in modern programming. Earlier programming languages such as Fortran, Cobol and Basic had input and output built in to those languages.

Modern languages like C, C++ and C# tend to avoid having this and instead implement input and output via library functions. Output involves sending a stream of byte to a device and whether that device is the screen, an object in memory, a file on disk, another computer on a network or a printer is unimportant. The same routines can be used with different destinations.

Categories: C# Tags: