Programmatically adding generated files to Project explorer - c#

I'm using T4 templates to automate creation of Poco objects. Ideally, these templates will run against multiple databases and will generate files going into separate folders based on where they came from. I already have the folder included in the project, is there anyway to add these generated files programmatically without knowing the names in advance?

After some more searching, I came across this answer which lead to me finding ProjectItems.AddFromDirectory , MSDN source here, which solved my question!

In Visual Studio, unload your project, then right click on it and edit the csprojfile.
You should be able to do something like this:
<ItemGroup>
<Compile Include="PlanExtract\*.cs" />
</ItemGroup>
Assuming that your generated files have a .cs extension.
Reload your project, then you should see the added files appear.

Related

.designer file not being associated with .cs file in visual studio?

EDIT:
There seems to be a visual (?) bug in visual studio. When I opened my website folder as a website and looked at my Views.ascx.designer.cs it doesn't show that it is associated. However, if I open that same website's solution file then the files are associated and all is well.
Just to be clear, even though it is still showing the below error messages it is working and I believe that it's a bug with opening it as a website instead of as a solution.
On my live site I have an error:
But it should exist because I have it in my Views.ascx.designer.cs:
But in Visual Studio my Views.ascx.designer.cs is not being associated with my View.ascx:
I tried to drag and drop the .designer file onto View.ascx but it displayed this error message:
It works and looks perfectly fine on my dev server:
I'm not sure how I would go about getting the file to associate itself with the View.ascx or View.ascx.cs files.
First step: right-click the files and exclude them, and try including them back in. It may fix itself. If not, you could edit the project directly; you just to add the dependency that the CSPROJ or VBPROJ expects. Here is an example that you need to make sure is in the project XML:
<Compile Include="Views\Main.aspx.designer.vb">
<DependentUpon>Main.aspx</DependentUpon>
</Compile>
<Compile Include="Views\Main.aspx.vb">
<DependentUpon>Main.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
Have had the same issue while adding form from other project to the pending one , I've excluded both files then added back by adding non-designer file 1st and .Designer.cs was then added in the right place automatically from the same catalogue as .cs file (VS2012).

Add created class file to project

I have a console application that builds some default classes for me from a database. When the files are built, I want to be able to refresh my folders and see the new files in my class library.
However no matter what I do the files don't show up unless I go in and manually add existing files. Is there a way for VS2010 to look at the file folder and add in anything that is in that folder to the project? For example:
Folder > File1.cs, File2.cs, File3.cs, File4.cs
VS2010 sees
Folder > File1.cs
How can I make VS2010 show these new classes?
Your problem is that you will only see files that are included and referenced in your .csproj file. This is by and large a good thing because it gives you ultimate control over what is taken into account in the project or not. This is causing you a problem though, because the created files which are inserted into your project directory aren't being referenced. As you have mentioned you can include the files manually, but I understand that you wish this process to be automatic.
The best way to resovle this in my opinion is instead of having a project create the files, use design-time T4 templates. Design-time T4 templates are files which resemble pre-Razor ASP.NET views, which allow code generation within your project. You can access your database, format your classes and then output .cs files directly into your project without building it. This is extremely convenient becuase it lets you work on catching compile-time errors that may come up based on the output without having to do a complete build.
More information about using T4 can be found here.
And a good walkthrough can be found here.
Haven't tried this personally, but you should be able to do it using this..
First gain a reference to your project using your apps' solution, then with the Visual Studio automation framework (DTE):
ProjectItems p = Project.ProjectItems;
p.AddFromFile("File1.cs");
Taken from: http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromfile.aspx
I would read further into it.
Select the project where you can find your file
On top of your solution explorer you can select "show all files"
Select your files and include
Adding them automatically can be done from another app or script by modifying your projects .csproj/vbproj file
<Compile Include="My Project\MyClass.vb" />
This must be done in the correct itemgroup.
I think this is not directly possible. You may write a template file (t4) in order to create you cs files and they will be added to project when the transformation file is run.
In order to run the transformation file after / before build, you may write a pre/post build event.
That will require you to create a VS add-in.. you can find an example here...
Okay so I have a console application that is building some default classes for me from a database.
Can't you let this application write all classes in one file, say Proxy.cs or Entities.cs. Then every time you regenerate the file and rebuild the project, you can access the new classes.

Generating Resx files for MVC

We use resx files for globalization, along with database lookups for things that can be configured (such as tab names, which can be different by product) by our CS staff, and thus aren't known at design-time.
I created a custom tool that reads resx files and intelligently dumps the key/value pairs into a relational database (matching values so we don't have duplicates).
This has been a big help to our business - we don't have to send each resx for translation (and pay for duplicate translations of shared words) and we have a 'gold standard' with all our translations (in the database).
The tool I created also reads the database, picking up the key/value pairs and the translations of each value, and creates text files for each resx file (and each language's translation of the text file) and automates running resgen.exe, a command-line tool that ships with Visual Studio, to compile the resx files from the generated text files.
I don't have any source-control integration, so we have to manually check out the resx files and manually check-in the generated files when using the tool, but this hasn't been a big problem.
My problem is that this method is failing for our new MVC projects: the MVC projects require the resx files to be embedded resources with the Access Modifier of 'public'.
Thusfar, we have been fixing this by hand, which introduces the possibility of human error and adds a non-trivial amount of work.
Is there a way to get resgen.exe to create resource files that are embedded and public? If not, is there another way I can create resx files that will do so?
Update, additional question:
The resx files we generate with this method also raise a warning:
A custom tool 'PublicResXFileCodeGenerator' is associated with file '(resxname)',
but the output of the custom tool was not found in the project.
You may try re-running the custom tool by right-clicking on the file in the
Solution Explorer and choosing Run Custom Tool.
The tool mentioned is the tool we initially use to create the resx files. Is there a way I can prevent this warning?
First of, you can generated resources as public by using the /publicClass command line option. Also see: Resgen.exe - Resource File Generator # msdn
Second, I don't think you can let resgen make the resource files embedded resources by default, simply because its not a property of a resource, but a setting of the project.
For example: when you add a new resource "Resource1", using the wizard, a new item group will be added to the project file:
<ItemGroup>
<EmbeddedResource Include="Resource1.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resource1.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
Maybe there are libraries to programmatically modify project files, but not that I know of.
What I would do is just try to serialize and deserialize the project file yourself, and add that section to it, for each resource your generate.
EDIT:
It will also add in a different Item Group:
<ItemGroup>
<Compile Include="Resource1.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resource1.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
</ItemGroup>
So, unless you have a good 3rd Party program to serialize, edit, deserialize the project file. It it probably better to let the wizard do it.
I have struggled with this before but have resourcing working well. This may help although I am not sure. Its always worthwhikle to go over the basics as sometimes its something very simple and .NET resourcing can be infelxible.
I will explain my use of resourcing and hopefully you can apply it to your scenario (I cant really figure out what they exact problem is based on your question.)
Steps to setting up resourcing
1. Add a ASP.NET Folder to your web solution right click on solution select Add > Add ASP.NET Folder > App_GlobalResources
Add a resource file to the folder call it MyResourceFile.resx
Open Properties for file and select
Build Action : Embedded Resource
Custom Tool : PublicResXFileCodeGenerator
Then add your different resource files for different languages etc (specify the same properties for all resource file etc Build Action and Custom Tool)
MyResourceFile.en-GB.resx
MyResourceFile.fr-FR.resx
MyResourceFile.ja-JP.resx
This should auto generate your resource manager which can be accessed through calling
MyResourceFile.MyResourceText
It worth noting that if you havent got a culture installed or its incorrectly defined it wont work and you get all sorts of errors.
Etc MyResourceFile.en-JP.resx would not work (unless you create this custom culture) and cause all sorts of other issues. This culture will be mapped from the CultureInfo in the application to determine which resource file to use, therefore it must be a valid CultureInfo.
We did end up finding a solution to this problem.
For MVC projects, we changed the tool to use the ResXResourceWriter class to update the resx files, matching existing keys and adding new ones as needed.
This preserves the 'Embedded Resource' status as well as all of the other details that are needed.
We still have to set the file up correctly the first time it's created, but that is a much more manageable problem.

How can I add Solution Folder files to a VS Project Template?

I'm trying to create a Project Template in Visual Studio. My basic Solution contains two Projects and a 'Solution Items' Solution Folder that contains (among other things) libraries (.dlls) that the two projects need as References.
My Project Template creates the two Projects, the 'Solution Items' Folder, and another Folder within 'Solution Items' (as desired). It does not, however, add the .dlls and other files that are supposed to populate 'Solution Items'.
In the .vstemplate file, I add references to the .dlls as 'ProjectItem' items directly beneath the 'TemplateContent' node, and they are ignored.
If I try adding a 'Project' node (as if my Solution Items folder were a Project), I get all kinds of errors.
Must I actually create a dummy project to hold these files?
It seems that I do need to include the Miscellaneous Files (Microsoft's term) in a Project in order to have the project generator find them. I put them into a folder and included them in one of my project Templates.
I then implemented an IWizard which I also included in the Solution Template which, after that Project had been built, would move the files where I wanted them, and modify the .csproj file so that it wouldn't miss them.
It all works fine, but it feels like the grossest of hacks...! Is anybody from MS out there?
I've never created a template before, so I can only guess at what you're doing. In our .sln files, our solution items look like this:
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Build", "_Build", "{45E29CA9-E213-4C73-AA54-AE9B87F79F2D}"
ProjectSection(SolutionItems) = preProject
_Build\Project.proj = _Build\Project.proj
EndProjectSection
EndProject
Where _Build is the SolutionFolder, which we map to a physical folder also called "_Build". In this case, it contains one file, the Project.proj file, which is of course mapped to the physical folder version.
Hope that's even remotely helpful..?

Referencing resource files from multiple projects in a solution

I am working on localization for a asp.net application that consists of several projects.
For this, there are some strings that are used in several of these projects. Naturally, I would prefer to have only one copy of the resource file in each project.
Since the resource files don't have an namespace (at least as far as I can tell), they can't be accessed like regular classes.
Is there any way to reference resx files in another project, within the same solution?
You can just create a class library project, add a resource file there, and then refer to that assembly for common resources.
I have used this solution before to share a assembley info.cs file across all projects in a solution I would presume the same would work fro a resource file.
Create a linked file to each individual project/class library. There will be only one copy and every project will have a reference to the code via a linked file at compile time. Its a very elegant solution to solve shared non public resources without duplicating code.
<Compile Include="path to shared file usually relative">
<Link>filename for Visual Studio To Dispaly.resx</Link>
</Compile>
add that code to the complile item group of a csproj file then replace the paths with your actual paths to the resx files and you sould be able to open them.
Once you have done this for one project file you should be able to employ the copy & paste the linked file to other projects without having to hack the csproj.
Some useful advice on how to manage a situation like this is available here:
http://www.codeproject.com/KB/dotnet/Localization.aspx

Categories

Resources