Reference a resource to be copied at build time - c#

In Visual Studio, I can add a resource (text file, image, etc.) to the project and have it copy to the output directory at build time. The problem with this is that the project copies the file into the project directory. I have a multi-project solution where many projects are referencing the same resource, I don't want the resource copied into every project directory. I want the resource to stay in a separate resource directory that I have under source control with the rest of the solution. Is there a way for me to add the resource to the projects and have the projects copy the resource to the debug and release directories on build that wouldn't force the file into the project directory?

In project explorer select the folder or project where you want to add the file and right click. Select Add Existing Item from menu.
Locate the file and click on the arrow near the Add button. From the menu opened, select Add As Link.
Do not forget to change the properties of the linked item (Copy To Output directory etc).

You should be able to link the files into your projects instead of copying them. See http://support.microsoft.com/kb/306234 for steps.

Related

Is there option to add file to the project in visual studio?

I'm new to c# and I only know the basics of it. I have some data in json file and I want it to be added to my visual studio without specifying the path of it (so other people don't need to change file location when they get my program). After building the project I want the file to be automatically in the project folder somehow.
Including file in the project and moving it to the output dir automatically are different actions.
To add an existing file to project, right click your project and select "Add Existing Item". Then open file properties (right click the file => properties) and set Copy to Output Directory to Copy Always

User manual text file not available in the exe version

I've just made my program an exe via publish in visual studio. In that i included a usermanual.txt and a aboutus.txt file which are in bin>debug folder. After i published the program and run it. Those files are not viewing saying cannot find the file. How can i fix this
Make sure your files are included in Solution Explorer. If not, add them (Right click on project -> Add -> Existing item... then select them from disk).
This way your manuals will be part of your project.
Then, you should setup that those files are copied to same folder as your exe (bin\debug or bin\release). To to that right click on them, select Properties and notice Copy to output directory setting. It has to have "Always" or "Copy if newer" option selected.
In your code, to open file, use path like this:
string userManualPath = Path.Combine(Application.StartupPath, "usermanual.txt");
that will open file in same directory as application's .exe.
When editing your manual (adding some new text), edit the one in solution, and the changes will reflect to either debug or release or published version.

ASP.NET: Publishing Website doesn't publish Resources folder

I have a website that I'm developing with ASP.NET. I'm using Visual Studio 2015. When I right-click and hit publish website the site publishes correctly except that my resources folder gets left behind. Heres what the solution explorer looks like in Visual Studio
But after I publish it here are the files that get put on Azure (accessed via FileZilla)
How do I tell Visual Studio to publish the Resources folder with the rest of the website?
Likely Answer
Open the Solution Explorer.
Right click one of the files in the Resources directory.
Choose Properties.
You now need to set two properties.
Build Action Content
Copy to Output Directory Do not copy
Do this to all the files that you would like to publish as content to the web server.
File Properties for Web Server Content
Remarks on File Properties
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. Not what you want. 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, that you do not want to publish to the web server.
Compile. Not what you want. The file is compiled into the build output. This setting is used for code files. In other words, we compile the file and the stick it into the bin directory.
Content. This is what you want. 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. The "Content output group" is a list of files that Visual Studio will publish while also maintaining the same directory structure.
Embedded Resource. Not what you want. This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files. In other words, it not only goes into the bin directory but is also embedded within a .dll or .exe file.
Copy to Output Directory. This property specifies the conditions under which the selected source file will be copied to the output directory. The output directory is normally the bin.
See Also
What are the various "Build action" settings in Visual Studio project properties and what do they do?
File Properties on MSDN
If like me you are using Visual studio 2019, just right-click on the folder and select publish "name of the folder"
Steps to add resources to be published (Visual Studio 2017):
1) Right click the resources folder and select "Include In Project"
2) Now you should see Build Action: Content on the properties for the images.
Make sure the contents of your Resources folder have the proper "Copy to Output Directory" property. Right click the files you want to copy over, select Properties, then in the Advanced section look at the value under Copy to Output Directory. Generally this is set to "Do not copy" by default since most things get packaged up in the .dll. Change it to "Copy if newer" to get it to bring over the file. It'll bring over the folder structure as well.

How to include a folder with files while publishing windows forms application

I have a windows forms application through which I am running SSIS packages. I have created SSIS packages and placed .dtsx files in a folder.
Now while I am deploying my solution using click one, how do I include these as application files?
Currently the project publish properties does not show these files even if I include it in my solution.
I cannot include these files as a resource file, because based on the user selection I will be selecting a specific .dtsx file say, entityname_targetname.dtsx as path and file name.
Because DTS.Application.LoadPAckage method expects a file name and not a resource name.
After adding it your solution, right click the respective file and choose Properties. Then select appropriate value in "Copy to Output Directory". This will add the files in the folder in which your main executable is placed after ClickOnce deployment.

Place Visual Studio resource files in a folder other than 'Resources'?

I have a resource file named rs.resx. In the Visual Studio designer, I can add an image to my resource file by clicking "Add resource" and specifying the path to my image file.
After adding the image, the image file itself is also copied to a folder in my Visual Studio solution named Resources. I would like all of my image files to be placed in a folder named Images instead. Is this possible?
This is a little tricky, but it is possible.
VS checks if the file added to a resource is already defined somewhere within your project. If it can't find it, it creates the folder Resources, puts a copy to the file there, adds this file to the project and puts a reference into the resource designer to this fresh copy of your file.
To avoid this behaviour you should add the file to your project before you add it into the resource file. If the file isn't somewhere within your project structure you can just create a folder, right click it, select Add file and before you click on the Add button of the OpenFileDialog, push on the little arrow next to the button and select Add as link.
Now the file resides on the place on your hdd wherever you like and the resource designer doesn't create a copy within your project file if you now add the file within the resource designer.
Maybe this little picture helps to find the Add as link button:
(source: modbusdriver.com)
That's just a subdirectory of your project directory. Your program doesn't use it at runtime, it should use the embedded resources. Anything you add to the .resx file gets copied there, not just images. But you can rename the folder if you really want to, right-click it and click Rename.
Instead of adding a .resx file to your project, I'd recommend you use the existing one. Project + Properties, Resources tab. Makes it very easy to retrieve the resource at runtime, just use Properties.Resources.Something in your code.

Categories