I have a C# WinForm application that has a few images on it.
I can specify the image location, but what I want is for the image to go with the file after it publishes. It still displays the one specified in the image location, so if a user doesn't have access to that location, he/she won't be able to see the image.
The magic word you're looking for is "resources". Have a look at the MSDN article on Adding and Editing Resources.
In addition to Thomas link, you can try the following:
Add an Image into your form. Change in the properties "Build Action" field. The Build Action property indicates what Visual Studio does with a file when a build is executed. Build Action can have one of several values:
None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
Compile - The file is compiled into the build output. This setting is used for code files.
Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.
Related
I would like to modify the contents of a file placed in my project folder (Specifically "Assets/recentChanges.txt") at runtime. Let's say it contains information about recent changes in application. It is supposed to be read and bound to a textbox control, which later on can be edited by developers and saved back. The problem with content files is that they are copied to output directories and therefore I'm not editing the original file in Assets folder, but the one in bin/Debug/Assets/ folder. And the Embedded Resource build action is not an option since such files cannot be modified.
The question is: how can I modify mentioned .txt file in its original root/Assets/recentChanges.txt location during runtime? All of the below instructions point to similiar build-dependant path:
Environment.CurrentDirectory;
Assembly.GetExecutingAssembly().Location;
Or, in other words, what would be the correct approach for implementing "Recent Changes" section with following conditions:
based on text file in its original project location
possibilty to modify the file at runtime through UI
file copied to the output folder and visible to the user (through textbox) in published application
I am aware that it's probably not the perfect approach, because "recent changes" info could be collected from database or the .txt file could be modified manually and then copied to output locations... but nevertheless I think the issue is somewhat confusing and I would love some help. Thanks!
The compiled and running application doesn't know anything about some "original" location. This location is only important to Visual Studio when the application is built.
If you want to modify the file in this folder you should specify an absolute path to it, i.e. "c:\your_project_folder\file.txt".
The .exe cannot be supposed to know from where it was compiled
I have a template service that uses RazorEngine to generate HTML for email sending.
I want to make it easy to add a new template to the project, meaning that I don't want to specify the file build action.
Is there a way where I still have the .cshtml file in my project with intellisense and etc. but it automatically will set their build action to Embedded Resource?
Here is a example of my project structure
Wildcard inclusions in C# project files
However, we can use wildcards when specifying these files. Here is an example that includes all .py files from a folder (and all subfolders):
EmbeddedResource Include="Python27\Lib\**\*.py"
Once you save the project file and reload it in Visual Studio, you will see that the Solution Explorer understands the wildcard and will populate with all files that match it. You can copy new .py files into the target directory and Solution Explorer will update accordingly (you need to click the “Refresh” button for this). And while it shows the results of the wildcard inclusion, it does not alter the entries in the project file unlike the behavior of the Add Files dialog.
I added picture box to my form and import 2 pictures, from properties under image property i choose the first picture when the application starting and inside my start button event i want to change my picture to the other picture.
this is what i have try:
pbIndicator.Image = Image.FromFile(#"..\Resources\indicator_green.png");
but file not found exception error occurs.
You should be able to do something like this:
pbIndicator.Image = Resources.indicator_green;
Be sure that in the property window if the Build Action is on Content, and Copy to Output Directory is on Copy if newer.
If you want it to be content. Else use the answer Shadow Wizard gave.
As I wrote in the comment if indicator_green.jpg is an image included as resource via resource file (Resources.resx) then it won't be copied to output directory (it means it's in your project folder because it's used to build executable but it'll be embedded inside your assembly, not deployed standalone).
Resource files will (by default) place resources you add inside Resources folder (and then linked). You can always access them using generated code file for resources:
pbIndicator.Image = Properties.Resources.indicator_green;
You may change namespace Properties and property name according to what you have in your project (by default property name has the same name of the resource and then same name as original file).
Of course you're not forced to embed your resources in your assembly. If you want to deploy them as standalone files just right click Resources folder and add an existing file. In the properties window for that file select Copy always for Copy to output directory and et voila, you'll be able to read it with:
pbIndicator.Image = Image.FromFile(#"Resources\indicator_green.png");
Please note that Resources folder won't be a sub-directory of your output directory (do not forget that source files are not part of installation).
Anyway I suggest you do not build path like that, little bit better would be to do not rely on current folder:
pbIndicator.Image = Image.FromFile(
Path.Combine(Application.StartupFolder, #"Resources\indicator_green.png");
You're not limited to Resources folder, you can do that with any folder (and with any name).
I have a project that reads several text files into a List via StreamReader, I have the files added to my solution under Resources, when I try to reference the file using StreamReader, I get a "FileNotFound" exception.
The files are being copied over to bin\debug\Resources, and the error says it's trying to locate them under bin\debug.. How do I reference them without using the literal path? (e.g. C:\users\etc) since when I compile it won't run on another person's PC if I reference actual path.
Code that calls text file:
using (sr = new StreamReader("FileName.txt"))
{
while (!sr.EndOfStream)
Names.Add(sr.ReadLine());
}
Under the text files properties I have it set to "Copy Always" and under Build Action it's set to "Embedded Resource".
Basically my main goal is to compile the project into an exe with the text files being referenced internally (their contents aren't changed by the program), so my application will be portable.
Error is clear. Change code to:
using (sr = new StreamReader("Resources\FileName.txt"))
{
while (!sr.EndOfStream)
Names.Add(sr.ReadLine());
}
In your situation, the error will be removed by just replacing the path Resources\FileName.txt
In other case, as you mentioned that you want to make a portable .exe. Then you need to embed files in your application. Now see how to embed files in your .exe :
Expand Properties in Solution Explorer
Double click on Resources
On left top of Resources tab, there will be a combo box
Choose Files
Now just drag and drop your files there
To embed files, you can go to properties of every file by right
clicking on it. Choose Embedded in .resx from Persistence
property.
To use the file you can use Properties.Resources.YourFile.
For more details, follow the link http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
I'm trying to add images to my tree nodes (ImageList.Add()), but just can't figure out a nice way of doing it.
I've read from MSDN help I should use System.Drawing.Image.FromFile(path). But cannot just get a file somewhere.
I'm building a DLL, and want it to be a single file, no bitmaps being copied together with it.
So I've read I should add Image files to the project and mark them with Build Action as "Resource".
Ok, but where do I get them??? I saw people using it in XAML files, but I don't have that.
Saw people using Resources.SomeName, but can't find those Resources class.
So....How do I do it?? I've got the files marked as resources, just need to add them to the ImageList.
By the way, I'd love to use the path relative to the Code File that is adding the images to the ImageList. But if not possible, just relative to the assembly root.
If you want to use file paths, for items that are in your project, you must set the "Copy to Output Directory" property to "Copy Always" or "Copy if newer", otherwise it won't be in the bin folder, and then you'll be trying to pass a path to a file that doesn't exist. Build action isn't all that important in this scenario.
If you want to use compiled resources, and reference them via the Resources object, see the rest of my answer. I assume you are using Visual Studio, 2005 or later.
To add an image as a compiled resource to a clean Windows Forms project, so that you can access it via Resources.SomeName do the following:
In Solution Explorer, under the windows forms project (mine is called WinFormsApplication1), expand the "Properties" folder. By default this folder should contain: AssemblyInfo.cs, Resources.resx, Settings.settings.
Double-click on the Resources.resx file. This will open an editor for it. You'll probably see a table of strings, with columns "Name", "Value", "Comment".
Click the drop-down arrow on the "Add Resource" button, and select "Existing File", which will allow you to browse to the image you want to add.
You should now see the image appear in a gallery of sorts. Mine has the name TestImage
Now when you edit the code (mine is Fom1.cs), I can access the image as a System.Drawing.Bitmap as Properties.Resources.TestImage.
To my mind, this is the best way to do images that you want compiled into the application. If you want user-added images, you'll need to use OpenFileDialog, or something like that to get your file path. Then the Image.FormFile() will be what you want.