c# have sound files in build - c#

I need to make a game for school.
I was adding some sound effects and music.
I have the paths to the .wav in the class and it will play them, but only when I start the game from the bin map in the project. I figgered out that the files are not included inside off the build. I started looking for it but could not find the correct way for it.
I have:
MediaPlayer _musicPlayer = new MediaPlayer();
string s = System.IO.Packaging.PackUriHelper.UriSchemePack;
Uri uri = new Uri(#"pack://application:,,,/{Assembly name};component/Resources/Sounds/Music/Main_theme_-_Thiago_Adamo.wav")
_musicPlayer.Open(uri);
_musicPlayer.Play();
This however does not work. If I add an messagebox with:
uri.IsFile.ToString()
It displays false.
On the file I set the propeties to be:
Build Action: Resource
Copy To Output Directory: Copy Always
I hope someone can help me.

Add the files to the project in your solution in Visual Studio, so they show up in the Solution Explorer (docked on the right side of the VS window by default). Click on a file so you see the properties window for that file (shown below the Solution Explorer in VS by default). Make sure the Copy to Output Directory property is set to either Copy Always or Copy if newer, and the Build Action is set to Content. Do this for each sound file you need.
At this point, the files will be copied to same folder as your application when you build it. They will not be packaged. They'll just be there in the file system next to your program. If you build the application in Debug mode, this is in the /bin/Debug folder (by default) for your project. If it's Release mode, it's the /bin/Release folder, but as long as your working directory matches your application you can reference them in the Uri by file name alone (otherwise you'll need to check the application path and build the full path to the file).
The reason you need Content instead of Resource is in the Remarks section of the MediaPlayer class documentation:
When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always.

Related

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

Load XML file into application output file

I have a folder in my main project that is separate than the Resources folder but also has some resources it that I would like to copy to the output folder as I reference it throughout the app.
I have set the build action to Resource, content, embedded content.. tried them all..
Also set it to always copy to output directory.
Now, from within my application I'm entering
AppDomain.CurrentDomain.BaseDirectory + "Dashboard\\Role.xml";
or also
"..\\..\\Dashboard\\Role.xml";
for the path and get an exception that says that the file can not be found.
Both of these paths work in my development machine but not once I deploy through click once.
I have tried to add it to the application files in the publish section as suggested in another post but it does not appear there.
I have also tried to put it in the resources folder and still nothing.... any ideas?
I followed some steps in this link and got it working:
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
Basically, you mark the file as build action "Content" and set it to always copy.
Then, you go to the application files in the publish section of your clickonce application and they will now show up.
Switch them from datafile to "Include" and you're set!

Application Executable Path C#

I've added an executable to my Visual Studio 2010 C# Solution. In the properties of this executable, the executable path is a full path ("C:\Test\MyProgram\MyProgram.exe")
When I deploy my solution (with installshield) on a new PC, the executable is part of the deployed solution together with some source files and the solution file. So far so good.
But when I open the installed solution file (in Visual Studio 2010),
I'm not able to build it because It can't find the executable in the specified path:
("C:\Test\MyProgram\").
Here is the question: How can change the full path of the executable, so it gets the path of where the solution is installed on the new PC. Something like :
"[InstallDir]\MyProgram.exe"
Thanks
Update: I found out that you can use relative path in Application's Executable path. Thanks for all your answers.
You could use TargetDir property
I am just thinking off the top of my head here. There may be a much simpler way. I'm thinking you might want to create a Custom Action that runs at the end of your installer that manually opens the .xxproj file, and manually edits the path of the reference. As another poster stated, you can get the new path from the TargetDir property: http://msdn.microsoft.com/en-us/library/aa372064%28VS.85%29.aspx
Example of creating Custom Actions: http://msdn.microsoft.com/en-us/library/9cdb5eda(v=vs.80).aspx
Add your Executable to your Project TO The Main Dir, right click-> Copy To OutPutDirectory -> Copy
this is easiest way to make your file to copy to your target dir, and have your SourceControl Visual studio plugin manage it.
Your executable should be be somewhere in your project source files structure, perhaps in a subfolder. When you add this executable file to project it should then be added on a relative path which is what you want. If this is not happening you should manually edit csprj file. To do this, right click on project, unload it, right click again and edit project file.
Of course, your executable file should have its property Build Action set to None and Copy to output Directory to what you want.
If for some reason you cannot add this executable directly into your project files structure I'd suggest to use pre-build event to copy it from where it exists into your project files.

Using text files in Visual Studio Project

I am making a console program in VS2010. The structure of as layed out in the solution explorer is Project file, Properties folder, References, and Program.cs.
I want to make use of a text file for reading/writing. What is the correct way of adding this file to the project? If I just do StreamWriter sw = new StreamWriter("maze.txt") then it'll create the file in the output folders (bin/debug or bin/release). But it won't show up in the solution explorer.
Now if I right-click on the solution explorer and Add New Item, I can create a text file in the root project folder (same level as Program.cs). This will show up in the solution explorer.
Is there a way for me to access this newly created file? Other than doing something like StreamWriter sw = new StreamWriter("../../maze.txt") by specifying the path to be two parents up?
How am I supposed to manage external files in a Visual Studio console applicatoin? In a WinForms application, there's a resources folder where I can add these things and a Resources.resx file to manage it. I can access it with Properties.Resources.someres.
If you want to create a file at design time and have it included in your bin folder then add the text file, go to properties, and select "copy always" or "copy if newer" for the copy property.
Note that this will copy the file from the project to the bin folder, but changes in the bin folder when debugging won't be copied up into the project itself. I doubt you want the program to interact with the version of the file in the project itself. If you do something like that then anytime you run the program outside of VS (i.e. when you copy it to the machine of an actual user) it won't work.
As for your resources question, you can still use resources. Just right-click and "Add" -> "New Item" (aka keyboard shortcut Ctrl+Shift+A) and choose "Resources File". You could also set the text file to be an Embedded Resource. MSDN has a lot of information on using resources on their Managing Application Resources page.
You can still embed resources to a console application. I've used this technique in quick apps so we don't need a installer or deploy dependencies such as these type of files. This question discusses a similar topic.

C# problem with Packing the supporting files with EXE, while publishing the project

I am using visual studio 2005, (.net version is 2.0+) to create a windows application. The functionality of the project is matching with the ideal design, there is just one problem in publishing the project.
I use MouseHover method to change the picture(image) used in intention to make attractive UI, when I hover the mouse pointer over the picture .. some other pic is loaded in-place of it .. and in mouseleave method the same picture is retained back.
Now the problem is while debugging this functionality works properly,
But when published, and used, the window won't load the image (as the installed folder doesn't contain these images) .. How to bind the supporting files like images, text files and any other files like xml with EXE??
I mean is there any ideal way to publish the project??
In your project, ensure that the images are set to Copy Always or Copy if Newer on the Copy To Output Folder property (F4).
That should ensure that when doing an XCopy deploy the images will be in the right folder (you will proabably need to change the logic for finding the image paths, so the application finds them in the right directory).
If you are using deployment project that will generate .MSI for your application, just right click on the project, click add files and then pick the images from your project directory.
Those files will be (by default) be deployed into the application target directory, and your end-user app will be able to access it.
You can package them as Embedded Resources.
To embed them, make sure that when the resources are selected in the Solution Explorer that the 'Build Action' in the Properties window is set to 'Embedded Resource'.
Here is an example with sample project on how to embed images : How to use embedded resources in .Net

Categories

Resources