Referencing resource files from multiple projects in a solution - c#

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

Related

How to share cs code among multiple solutions?

I'm using VS 2017 and I have a number of Utility class files (NetworkLib.cs, ImageLib.cs, etc) that I find myself constantly having to duplicate over and over again when creating new solutions.
Normally, I would just end up creating a dedicated separate solution, NetworkLib.csproj, compiling these into DLL files, and then adding a DLL reference whenever I need them in a new solution, but this isn't ideal for me.
I want to be able to use these in a shared library sense across multiple solution files but also when debugging if I step-into some function NetworkLib.Post() the debugger should step into the appropriate CS files. Additionally, if I make some changes to NetworkLib.cs from Solution1, all other solutions should pick it up on rebuilding.
I've given thought to creating a standalone Utility solution with a NetworkLib csproj, and then adding the project csproj as an existing project to each solution, but not sure if that's a good approach.
I also just read about the "Shared Project" but when I created it (in its own standalone solution) I can't seem to import it into other solutions. When I look in "Shared Projects" tab it just states "No Items Found".
Normally, I would just end up creating a dedicated separate solution,
NetworkLib.csproj, compiling these into DLL files, and then adding a
DLL reference whenever I need them in a new solution
That's the solution. You should publish your shared DLLs into a "shared" folder, somewhere on your desktop/server/network/... and reference these DLLs in your solutions. If you rebuild the shared library, every solution referencing it will automatically use the new version.
To debug external libs, you will need the .pdb file generated alongside with the .dll file. Look here for complete answer : How to debug external class library projects in visual studio?
Use "Linked items" - where the .csproj project has a soft reference to a *.cs file (or other file type) located anywhere in the filesystem, including outside of your source-control workspace or even a network share - just so long as the file exists your project will build. Use this technique judiciously because it's easy to break CI/CD systems for obvious reasons.
"How to: Add Existing Items to a Project": https://msdn.microsoft.com/en-us/library/9f4t9t92(v=vs.100).aspx

How to split an assembly file(.dll file) into many assembly files

I am building a projects by C#. It contains many folders, each folder represents for a function. Now I want to make a .dll file for each of them. Is there any tools can help me to do it?
And after that I want to merge them together. Is there any solution for me to do it?
Thank you,
You are searching for modules in .NET. Where module is a file (not a DLL) with self sufficient (from structure perspective) content which can be read and executed by CLR virtual machine into machine code.
Read this for detailed explanation: How to: Build a Multifile Assembly
You will need to do a little command line execution, no direct visual studio support, afaik.
You might want to move from a folder structure to a project structure. Each project will automatically have its own .dll file. Then you can have a central project that has references to each of them.
It would be a good idea to group together certain code files by project for other reasons too. Suppose you have a lot of extension methods that could easily be re-used for other coding projects. You might separate these classes into a Util project / namespace for easy re-use and access across other projects.

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.

Share a file for C# project at home and at office

I have formed a number of source code files as my library. For example, I wrote LinqExtension.cs providing Median() function.
Now I'm working on a project which needs LinqExtension.cs. As usually, I link the file to the project. As introduced here. The reason that I link files rather than copying them is to keep the files at a single location. If I modify a file, all dependent projects get affected.
I also add the project to Subversion and upload to and download from Google Code. The linked file is not under version control.
I work on the project at home as well as at office. I hate copying the linked file to my office, which makes the file not single.
I figure out a solution that add <Compile Include="http://www.example.com/LinqExtension.cs"/> to csproj file so that the file only exists on the Internet. Once I upload a new verison of the file, all dependent projects get affected. Unfortunately the solution doesn't work.
Any other suggesions or better practice?
A better way would be to share your core library at the binary level, rather than at source code. You could set up a private Nuget repository to make this easier.
If it is absolutely necessary to share files, you can use pre-build actions in your project to copy the file from a common location, or even download them from google code. It's not clean, but if you don't want to use source control for it then I don't think you will find a clean way.
I like to keep a library folder of binaries in my Dropbox. That way Common libraries that I use can be accessed from my home and work project workspaces and the service keeps the version up to date.

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.

Categories

Resources