Embed the content from an external file when compiling - visual-studio

Is there a way in Visual Studio (any version) to embed the content of a file in another file upon compiling? For instance, if one wanted to embed an xml file in a vb code file how would it best be done?

Add the file to your project, right click on the file and select its properties. Under "Build Action" change it to "Embedded Resource". Now when you compile the file is automatically embedded as a resource.
Here is an example showing how to access an embedded bitmap resource.

Have a look at Resources. You can create a string resource that has your xml. This is then compiled into your application image.
Brian beat me about the embeded resource as I was looking for the resource URL :)

If you don't mind it actually being in your code, you can use an XML Literal.

I was about to mention using the #include preprocessor directive (for C and C++), but then you mentioned VB. I don't think VB supports such a thing.

I have no idea on how to go about doing this. But the first thought that came to my mind was that of Build Providers (the thing that generate classes from xml file in case of an ORM).
I searched & found 1 more thing called Custom Tools, which can act on a file existing in your solution and can be used to generate code file.
See if this link helps at all - http://www.drewnoakes.com/snippets/WritingACustomCodeGeneratorToolForVisualStudio/

What I've done in the past is write a simple .bat file that concatenates 3 different sources into a final source file that actually gets compiled. The .bat file is run as part of the Pre-Build event.
Project
SourceTop.vb
Source.xml
SourceBottom.vb
All of these files have a Build Action of "None"
Merged.vb
merge.bat
type SourceTop.vb > Merged.vb
type Source.xml >> Merged.vb
type SourceBottom.vb >> Merged.vb

Related

Adding syntax highlighter to JamWiki-1.2

The tutorial http://sinnerinc22.blogspot.de/2010/07/adding-syntax-highlighter-to-jamwiki.html describes how to enable syntax highlighting in JAMWiki.
My problem is that in the recent version of JAMWiki v1.2 the two files to be modified WEB-INF/jsp/top.jsp and WEB-INF/jsp/close-document.jsp do not exist any longer...
There is a third-party syntax highlighting tag extension available with JAMWiki 1.2 link that may work for you
I have added SHJS to my installation just following SHJS instructions rather than JAMWiki instructions. Simply edit JAMWiki JSP pages to add content as documented here. To see how does it work, look into the source code of this HTML document.
You even do not need to compile anything after you edit JSP, the server does this for you automatically.
Following up on Audrius's answer, here's exactly what you need to modify.
./jamwiki.war/WEB-INF/jsp/topic.jsp Modify it to look like this
Download the SHJS zip and copy all of the individual files from ./css/, ./lang/, ./sh_main.js, and ./sh_style.css from the zip to JamWiki.war/shjs/. (This will flatten the directory structure so everything is now in ./shjs/. Flattening the structure is optional but it makes for easier paths when referencing them in the jsp.)
You can modify the .war with 7zip or dig into your web app container file system and place the JSP and shjs folder directly.
Redeploy or refresh as needed depending on your preferred edit method.

VS2010 not embedding files into assembly

For some reason in VS2010 I can't embed any file into an assembly. These files have their Build Action property set to Embedded Resource as they should, but when the assembly is executed it founds no resources. For example, the following test code:
string[] list = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
string msg = (list.Length > 0) ? "Full" : "Empty";
MessageBox.Show(msg);
always shows "Empty" because GetManifestResourceNames() always returns an empty list.
This issue affects a project using nettiers in which is not convenient for that particular case to include the stored procedures into the DB, so they must be taken from the Procedures.xml file, which BTW is automatically set with Build Action= Embedded Resource after the classes generation, and then of course when a function tries to get any SQL script from it, the program fails.
I also created a couple of test projects where I tried to embed an xml and a gif, always with no success.
Update: The problem seems to be only for C#. I reproduced the test project in VB.Net and there the file does get embedded. I've noticed that in the output window, in the C# case, the compiler command line doesn't include the /resource option (which should be there followed with the name of the file to embed) whereas for the VB case it does appear.
Are there any global settings or configuration files anywhere for C# I should check? (The build options for the project have nothing strange there)
Does anyone have an idea of what may be happening and how to fix this?
Finally, I found the cause:
There was a wrong line inside the Microsoft.CSharp.targets file (located inside c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319). There was a tag written as:
<EmbeddedResourceF Include="#(_Temporary)" />
instead of
<EmbeddedResource Include="#(_Temporary)" />
Writing it right restored the ability to embed files
How that F appeared and why that file got altered in first place, I dont know... Despite being a .NET programmer for several years I just became aware today of the ".targets" files after searching the web about this issue, as never have had the need to look about the MSBuild stuff.

Output Path for Shared T4 Template

How can I specify the output path of a T4 template?
When I add my T4 template using "Add as Link" to my project from $TemplatePath$, it generates the output file in $TemplatePath$, not $ProjectPath$. How can I specify that $ProjectPath is where the generated file is placed?
I got around this by having a master template in one location and then templates that imported that one (but did little else) in each project. Of course, that's not the nicest solution.
We typically use the same system that Jeff Yates mentioned with minimal (often single line) stub templates including the shared template.
You could also use Oleg Sych's T4Toolbox to project output to any directory you please.
However, the standard IVsSingleFileGenerator that T4's custom tool is built upon doesn't have any dials to turn to control it's output location and it's behavior in the case of linked files is as you describe.

How to use a manifest embedded as a resource? (Windows XP/Vista-style controls)

I have a manifest embedded as a Windows resource. But what's the right way to apply it?
More specifically:
I have my .manifest file. It's the standard one to make an app use controls in the Windows XP/Vista visual style. When applied by being in the same directory as the .exe and suitably named, it works.
However, this approach is rather cumbersome. I'd like my .exe to be self-contained. Clearly it can be done, given that many of the apps on my computer manage to use the modern style without a separate manifest file.
So I had a look around, and finally found embedding the manifest as a resource. So I've added this line to my .rc file:
1 RT_MANIFEST "mdiedit.exe.manifest"
But what do I do next?
Edit: If it helps you to know, my program is written in D and built using the Digital Mars D and resource compilers. No IDE. But I'd expect any solution that works in C(++), when built using command-line tools, to work in D as well.
RT_MANIFEST is a define, so make sure your resource file includes the correct header, or just replace RT_MANIFEST with 24
Did you add the .rc file to your project yet? Alternatively, use your development environment's resource compiler, such as brcc32.exe or rc.exe, to compile the .rc file into a .res file, and then add that file to your project.

How to make file show when opening Visual Studio solution

I need to add a C# solution with examples that would be distributed as part of a software library installer. This solution would have various examples on how to use the product's API.
I want to be able to display a simple "quick start" file explaining how to run the examples when the solution is opened in Visual Studio.
Is there a way to tell Visual Studio to open a specific text file when the solution/project opens?
It sounds like a solution or project template would be the best option. This would let you create an entry in the user's File - New dialog (Similar to 'New Class Library" etc). In VS 2008, these are easier to create - File -> Export Template. The template is just a zip of the project(s) with an xml manifest file you can modify. Part of the manifest schema allows you to specify files to open as HTML or text. The templates can be installed relatively easily as part of a installer package.
Here's more on the general concept:
http://msdn.microsoft.com/en-us/library/6db0hwky.aspx
And schema reference about how to open files in various modes on startup:
http://msdn.microsoft.com/en-us/library/ys81cc94.aspx
If you need to provide more guidance/wizards, consider Guidance Automation Toolkit.
What Will said.
The UI state of the solution (e.g. which files are open for editing) is stored in one of the solution files of which there's supposed to be a separate copy for each user, and which therefore isn't usually checked-in to the shared version control: i.e. not the *.sln file but instead I think the *.suo file (but beware, this is a binary file which won't 'merge').
I don't think it is possible to have a solution file open specific content or even script actions, actually.
Perhaps you could create an MSI setup for your library (if you haven't already) and not deliver a solution with example code, but a project template that is installed by the MSI in the right place to be instantly available as a template in VisualStudio? Then someone can easily do "New Project", select the demonstration template and get a project preset with your example code.
Just make a .bat file (using the VS env) with that calls devenv /useenv yoursolution.sln - this way you can make things a bit fancy if you want to ;)

Resources