Some upvoted misinformation here. (Chris's answer is good, as usual.)
If you are going to distribute a copy of an application, and that application needs external files as "resources" (like sound, images, databases, external text, etc.) then you need to include those files along with the executable;...
Best answer: Some upvoted misinformation here. (Chris's answer is good, as usual.)
If you are going to distribute a copy of an application, and that application needs external files as "resources" (like sound, images, databases, external text, etc.) then you need to include those files along with the executable; and find a way to put them someplace where your program can find them when it runs. That's part of what's called "installing" a program.
The program certainly can't read from drive D: on your computer, when it's running on another computer altogether. With C#, it might not even be on a Windows system at all. The Mono platform is now supported (and sponsored!) by Microsoft.
The simplest approach fix this is Chris's suggestion. Put them in the same directory as the executable. One easy way to do this is to pack all of the needed files in to a ZIP archive file that will keep all files together.
Where Windows (or Linux, MacOS, etc.) looks for files by default is not as simple as suggested. It depends on how the program is launched. The default location when run by double-clicking an icon in the file explorer (or on the desktop) is to use the same location as the exe file. That's what Chris is assuming.
If you run from a Command Prompt or terminal window, it uses the current Windows default drive and path. If you make a Windows shortcut ("icon") to the executable file, that can point to any directory you like as the "startup directory". If you want to handle this situation gracefully, you can use a few standard .NET tools.
To get the full path and file name to the current running program:
string ExeFileName = System.Reflection. Assembly.GetEntryAssembly().Location;
To get just the directory name from that:
string ExePath = System.IO.Path. GetDirectoryName(ExePath);
Now you can open (ExePath + "\\file.wav") as the name of the file containing the sound. Don't forget the \ separator (doubled as \\ to get a single \ in the string.)
Windows exe files can contain resources, and there's a hint about how to do that with a sound file here:
If you're using an Express version of Visual Studio, you might have to "upgrade" to the Community edition to get full support for resources.
A few things from earlier answers:
Windows does NOT use any environment variables to find directories of data files. There's a PATH variable for finding executable programs when no directory was specified, but that's about it.
There is no "current directory" entry in the environment...not one set and managed by Windows, anyway. Command Prompt adds multiple fake environment variables, such as PROMPT, TIME, ERRORLEVEL and others, includiding CD for the command processors current drive and directory. The can be used at the command line or in batch files, but are internal to the command processor and not visible to running programs.
7 days ago