Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GetManifestResource problem

GetManifestResource problem

Scheduled Pinned Locked Moved C#
helpquestionlearningcsharpvisual-studio
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JustRonald
    wrote on last edited by
    #1

    Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

    L D G A 5 Replies Last reply
    0
    • J JustRonald

      Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      JustRonald wrote:

      But I can't get it to work properly.

      JustRonald wrote:

      but i'm just a beginner and can't figure it out.

      If you are getting some error message you need to post that along with the relevant code. Read the first post in the forum titled "How to get an answer to your question".

      led mike

      1 Reply Last reply
      0
      • J JustRonald

        Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        There's an example here[^] The only problem I've found is you need to specify the file's location as "yournamespace.Resources.filename.ext" e.g. GetManifestResourceStream("My_App.Resources.new.wav"); Edit: You needto use Winmm.dll not Coredll.dll Improved solution/example below

        Dave

        modified on Monday, March 3, 2008 2:51 PM

        1 Reply Last reply
        0
        • J JustRonald

          Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          Here is an example: Convert mp3 to exe[^]

          #region signature my articles #endregion

          1 Reply Last reply
          0
          • J JustRonald

            Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            I thought this was something interesting that I could maybe use so I've had a play and the example below works (ringin.wav from C:\Windows\Media added to resources and BuildAction property set to Embedded Resource).

            using System;
            using System.IO;
            using System.Reflection;
            using System.Runtime.InteropServices;
            using System.Windows.Forms;

            namespace WindowsFormsApplication1
            {
            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            Load += new EventHandler(Form1_Load);
            }

                void Form1\_Load(object sender, EventArgs e)
                {
                    Stream wave = Assembly.GetExecutingAssembly().
                        GetManifestResourceStream(
                        MethodBase.GetCurrentMethod().
                        DeclaringType.Namespace + ".Resources." + "ringin.wav");
                    Byte\[\] waveData = new Byte\[wave.Length\];
                    wave.Read(waveData, 0, (int)wave.Length);
                    PlaySound(waveData, IntPtr.Zero, SoundFlags.SND\_ASYNC | SoundFlags.SND\_MEMORY);
                }
            
                enum SoundFlags
                {
                    SND\_SYNC = 0x0000,
                    SND\_ASYNC = 0x0001,
                    SND\_MEMORY = 0x0004,
                    SND\_NODEFAULT = 0x0002,
                    SND\_LOOP = 0x0008,
                    SND\_NOSTOP = 0x0010,
                    SND\_NOWAIT = 0x00002000,
                    SND\_FILENAME = 0x00020000,
                    SND\_RESOURCE = 0x00040004
                }
            
                \[DllImport("winmm.dll")\]
                static extern bool PlaySound(byte\[\] pszSound, IntPtr hmod, SoundFlags fdwSound);
            }
            

            }

            I've used SoundFlags.SND_ASYNC as it's in the load event but if you want it in FormClosing for example use SoundFlags.SND_SYNC

            Dave

            1 Reply Last reply
            0
            • J JustRonald

              Hello, I'm building an application which has an option to play a sound with the extern PlaySound method. With the code below the PlaySound method is working string fileName = @"C:\Documents and Settings\User\My documents\Visual Studio 2005\Projects\Alarm\Alarm\Resources\attention.wav"; PlaySound(fileName, 0, 1); If I execute the program on an other computer the path will be different. So I need an function that will get the .wav file from my folder Resources. The standard way to access an embedded resource is to use the Assembly class's GetManifestResource* methods. But I can't get it to work properly. This is proberly a very stupid question but i'm just a beginner and can't figure it out. Can someone please help me out? Thanks

              A Offline
              A Offline
              ADLER1
              wrote on last edited by
              #6

              Hi Ronald, there are some ways to solve your problem: 1. you can distribute your wav file in a specific subfolder of your application, e.g. Sounds. Then you just set the filename variable to the following value: string filename = Application.StartupPath + @"Sounds\attention.wav"; Don't forget to include using System.Windows.Forms. or type the namespace with the Application.StartupPath 2. You add the audio file as manifest into your exe. Then you need to call the following function: System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Program), "attention.wav") Note that you get a System.IO.Stream, so maybe need to save it in a temporary file and then call your PlaySound method (I don't know if your function supports a stream object as a parameter) So, these are the ways, I retrieved quickly from my mind at 23:17 :) (German time) PS: In the second solution I wrote typeof(Program). Program is a class generated by VisualStudio when you create a new project, so I used this here :) I hope I could help you

              _____________________________ The force of .NET is with me!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups