Basic doubt in C# (Sounds)

Asked

Viewed 84 times

0

I am new in the forum and new C# programmer also.

I made a basic software for college; a coffee maker. and in it I used sounds, but I indicated the sound in ("C:/users/...") the path on my PC.

When I do the ". exe" of this application, when I run on another pc other than mine it gives miss on sound and closes.

I know the question is stupid, but how do I fix it?

1 answer

6


Instead of using the direct path from the PC, creates a folder at the root of the project called "Sounds" all sounds that will use in the application you play it.

When using the sound you search as follows:

//Monta o caminho dos sons
var caminhoSons = Application.StartupPath + @"\Sons";

//Valida se a pasta existe
if (!Directory.Exists(caminhoSons))
{
    //Se não existir cria a pasta na raiz do projeto
    Directory.CreateDirectory(caminhoSons);
}

//Pega o som especifico
var musica1 = caminhoSons + @"\Musica.Mp3";

The command Application.StartupPath get the folder where your project is running, then it can be on any PC, even on a Pen Drive (E:,F:) that it will get the correct project path, and then having the sounds folder inside the project folder will always find the right one.

  • Sensational! It worked perfectly here! Very grateful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.