Windows 窗体应用程序上的音乐位置问题

问题描述 投票:0回答:1

此代码正在运行:

Music sound = new Music();

sound.Play(@"C:\Users\itama\Documents\Startech\HelperApplication\HelperApplication\data\music\alarmSound.mp3");

我有一个问题,当我更换计算机/当有人在另一台计算机上安装我的应用程序时,此位置不起作用,我该怎么做。如何使该位置适合安装在任何位置的任何电脑?

我试着这样写:

sound.Play(@"HelperApplication\data\music\alarmSound.mp3");

它不起作用

我可以像Properties.Resources.filename.mp3这样写吗?

c# windows winforms
1个回答
0
投票
// In your example work code write this before your code

string path = string.Empty;
try { path = System.IO.File.ReadAllLines("mp3path.txt")[0].Trim(new char[] { ' ', '"', '@' }); }
catch { }

// And rewrite your code

Music sound = new Music();
sound.Play(path);

// In summary

string path = string.Empty;
try { path = System.IO.File.ReadAllLines("mp3path.txt")[0].Trim(new char[] { ' ', '"', '@' }); }
catch { }

Music sound = new Music();
sound.Play(path);

// Compile your project to an executable-file.
// In the same folder in the same place as the executable-file of the program,
// create a text-file "mp3path.txt", and in this text-file indicate the path to mp3-file.

// You will have an executable-file, like "program.exe", and in the same folder
// there is a text-file "mp3path.txt", and in this text-file write path, for example:
// "C:\MyMusic\AlarmSound.mp3"
// And the path that you write in text-file "mp3path.txt" is the path for mp3-file.

// What you need?
// executable in folder
// mp3 in folder
// text-file in folder
// and path to mp3 in text-file
© www.soinside.com 2019 - 2024. All rights reserved.