How to build a stand alone exe with resources? - c#

I have a WPF app with image resources. When I build the program, there is a directory named "resources" in the bin folder containing my images. If I run the program without the folder, it crashes. How can I embed the resources in the app so it can run stand alone?

You have to set the build action of the image files to Resource, like shows here. The Uri for an image file named Image.png would then be this:
pack://application:,,,/Resources/Image.png
See also the MSDN page Pack URIs in WPF.

Related

WPF accessing a resource from anywhere in the assembly using relative path

In my application I am creating a ton of custom controls that I'm then using in different windows. The structure of my project looks something like this:
Controls (folder)
MemberList (class)
Resources (folder)
Windows (folder)
Window1.xaml
Window2.xaml
Helper windows (folder)
Window3.xaml
I am creating classes (controls) that I store in the Controls folder. If I want to use an image in the control, I access it like so:
new Uri("../Resources/my_image.png", UriKind.Relative)
Sometimes though, I want to use the same control in multiple windows and sometimes these windows are stored in different folders. If I use the control in say Window3 that is stored in another folder, the image is not displayed.
If I change the Uri to this: (adding another "go back" command in the path)
new Uri("../../Resources/my_image.png", UriKind.Relative)
the image is being displayed in the Window3, but it is not displayed in the Window1 or Window2.
Is there a way to create paths that are relative to the main executable and not relative to the window that is displaying the control?
EDIT:
I forgot to mention one important part. Because of the fact that while starting this application I was just learning how to use WPF, I messed one thing. My resources folder is not in the folder that contains the main executable.
My file structure looks like this:
bin
Debug
MyApplication.exe
Resources
I was trying to use the path that everyone suggested:
new Uri("pack://application:,,,/../../my_image.png", UriKind.RelativeOrAbsolute)
but I get the exception:
Cannot locate resource 'my_image.png'.
I could try to relocate the Resources folder, but it would mess up all other paths. Is there any way to go "back" in folder structure in the "pack" uri? If not, what would be the best way to relocate a folder so all paths do not get messed up?
Use a "pack" uri...so you would use:
pack://application:,,,/Resources/my_image.png
so like this:
new Uri("pack://application:,,,/Resources/my_image.png");
to refer to your "image" from ANY of your Windows/Controls.
If you will have other "projects"/"assemblies"...which you want to be able to refer to the resource then use:
pack://application:,,,/AssemblyNameContainingResource;component/Resources/my_image.png
And when you are adding your images in your Resource folder....make sure you use Resource as the Build Action.
Use Pack Uri Scheme
Resource File Pack URIs - Local Assembly
The pack URI for a resource file that is compiled into the local
assembly uses the following authority and path:
Authority: application:///.
Path: The name of the resource file, including its path relative to the root of the local assembly project folder.
The following example shows the pack URI for a XAML resource file that
is located in the root of the local assembly's project folder:
pack://application:,,,/ResourceFile.xaml
The following example shows the pack URI for a XAML resource file that
is located in a subfolder of the local assembly's project folder:
pack://application:,,,/Subfolder/ResourceFile.xaml
Short answer: Use pack URI notation.
Example:
Your URI would look similar to: pack://application:,,,/Resources/my_image.png regardless of where in the XAML you use it.
Documentation:
MSDN - Pack URIs in WPF
In a submodule such as SubModuleA, in the viewmodel of SubModuelA, when you want to access the resource file of SubModuelA instead of the started project.
For example, the resource file's directory is StartApp/SubModuelA/Resource/Data/JsonData.json, In viewmodel of SubModuelA it's path should be
string jsonFile="../../../SubModuleA/Resource/Data/JsonData.json"
I validated this and wrote data into the jsonFile.

How can i merge exe file with dlls and other files?

I have a simple c# project that contains a form, a button and an axWindowsMediaPlayer that shows a video.
My video file is in bin/debug folder.
Now, i want to create an exe file and run it on other machines without install.
How can i merge or embed all dlls and exe and video files in simple exe file and run it on other machines?
my debug folder image :
Thanks in advance
A self-extracting exe will give you a single file for distributing your application to other machines, however, if your goal is to end up with a single exe file after installation, then you could embed the dlls and video files as embedded resources in the main exe. To do this, right click on a file in solution explorer and in the properties tab select Embedded Resource for the Build Action. Depending on the type of resource you're embedding you may need to modify your code to access it.

clickonce deployment setup , gif image not working after install in a different computer

I am trying to deployment a setup for my WPF c# application. i suppose to do it using clickonce because it allow to auto update when i do any update in my application.
Actually i create a setup for my application. when i install that app in a different computer, it works...when i do some update my app, it automatically detect when program launching and user can install new update and run the app.
But it does not show any gif images when program running. when i install the app in a different computer, it create a desktop shortcut and start menu icon. when i run the program via shortcut or start menu, it does not show any gif images. it work fine without showing gif images.
app installed in
C:\Users\dilan\AppData\Local\Apps\2.0\TA5EZNVO.D56\TY5K2KDD.7B6\rcu_..tion_d1f05f20b6f0835a_0001.0000_d9a8ddd982f2efb7
i went to this location and double click the exe file. then app is running with showing gif images.but it does not update. update only happen when run the app via desktop short cut or start menu.
i tested the app by putting image files in to Images folder , Resources folder...but it does not work..
i think i missed some points when creating setup file. can anyone help me , how to deploy a setup file using clickonce , that app work with gif iamges also.
Here is how XAML looks like:
<Image gif:AnimationBehavior.SourceUri="pack://siteoforigin:,,,/Images/tapcard.gif"
VerticalAlignment="Center" Width="100" Margin="0,0,20,0"/>
I might not have inculded your images in your project.
See this answer. You need to right click the file in Solution Explorer and then Include in project .
When you include them, the Build Action of the image file would be set to Resource - no need to copy images individually (Copy to Output Directory should be by default set to Do not copy).
Let's say you have a WPF project in the folder Images named Progress.gif.
In code behind you reference it like this:
new BitmapImage(new Uri("pack://application:,,,/Images/Progress.gif", UriKind.Absolute))
In XAML you reference the image simply like this
<Image Source="./Images/Progress.gif" >
You can use another URI scheme as well, e. g. pack://siteoforigin:,,,/Images/tapcard.gif refers to an image that is located on the server where the app was deployed.
If you do not want to be bound by the restrictions of having your
application resources declared at compile time, there is another
option for you. No, this doesn't involve using fully qualified Uris to
reference resources over the internet. Although, that is indeed
supported. WPF provides you with an abstraction for the application's
conceptual site of origin i.e. the location from where the application
was deployed. For instance, if your application was launched from
http://nerddawg.blogspot.com, then your application's site of origin
is http://nerddawg.blogspot.com. To access an image at
images/AuntDahlia.gif at that location, you would specify in markup:
<Image Source="pack://siteoforigin:,,,/images/AuntDahlia.jpg" />
So you check in the properties of your WPF project on Publis tab the Publish location or Installation folder URL (if specified). If installation URL is e.g. http://myserver:8080 then you need to verify from the client, that the file http://myserver:8080/Images/tapcard.gif exists by typing this url into a browser. Depending on the folder structure of your web you might need to copy the file manually there.
pack://siteoforigin: means you are getting the resource over the network from the web or file server.

How to copy a file to output folder without modifications? Visual C#

I would like to publish a project with a mp3 file in the application folder, so that I can use it when app is running. Is there some way to do it?
My current attempt is to click on the mp3 and drag it to the "Solution Explorer". When the program is published, the output folder does have a "play.mp3" file, but it is named "play.mp3.deploy", which turns it unusable
If this is a forms app then add the mp3 file to a resource file (resx) and access it that way.
If the app is a website you should be able to mark the file properties as Build Action = Content and Copy To Output Directory = Copy Always. This should then just get deployed in the same manner a js file would for example.

Loading Content images from Blend from separate project

I have a VS2010 solution with 5 projects. Two of these projects are called:
MyResources
Application
The Application project contains all of the views and viewmodels for the application to run, and the Resources project contains all the images, resource dictionaries, etc for theming and skinning.
The way I have images set up in Resources, I have their build action set to Content, and Copy set to Copy Always. In my Application project, I reference these files using a pack URI. Example:
"pack://siteoforigin:,,,/Themes/DefaultTheme/BackgroundImage.png"
This works fine for me. The themes folder and all its subfolders are properly copied to the proper folder since I'm using siteoforigin://.
However, this breaks Blendability. When I load the project in Blend 4, I get errors.
If I use siteoforigin, Blend tries to load the images from the executing folder of blend.exe (In this case, C:\Program Files\Microsoft Expression\Blend 4 Beta\Themes\Default_Theme\BackgroundImage.png")
If I change the pack URI to the following:
pack://application:,,,/IQ.IQKiosk.Resources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "themes/default_theme/backgroundimage.png"
If I try:
/MyResources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\MyResources;component\Themes\DefaultTheme\BackgroundImage.png"
If I try:
/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\Themes\DefaultTheme\BackgroundImage.png".
So now I'm stuck. I'm not sure how to reference my images without having to embed them into the resource project, and I can't hard code the directory because my other coworkers have the project in different folders as well.
I tried to think of a way to get the proper URI to the proper locations of the files (relative to the .sln file would be awesome) or have the files get copied over to where blend compiles and executes the project. Alas, I am unable to figure out how.
Does anyone know the proper way to get these images to load?
I figured it out. Instead of using a pack URI, I just used
new Uri(Environment.CurrentDirectory + "\\Themes\\DefaultTheme\\BackgroundImage.png")
and it found the proper folder, since Evironment.CurrentDirectory gave me the proper bin folder where my themes were copied to.
Huzzah!

Categories