Hi There: I have 2 projects in a solution. Project A and Project B. B has a reference to A. Thus A uses B's class and functions. B does not have any resource files as it contains all business functions only. A contains all the resource files. I need to use a resource file from An into B. I CAN NOT refer to A in B.
I deploy my main project and it has referred to B. But how can I refer to a resource file(of A project) into B project without referencing the A library in B.
Thanks in advance
Wow, I finally found it from searching online.
I Linked the resource file in Project A to Project B.
Right Click Project B and "Add-Existing Item" then Browse the resource file in Project A
(myresrc-en.resx and my myresrc-fr.resx)
Add a Link for both. Now in Project B
ResourceManager rmg=new ResourceManager("B.myresrc", Assembly.GetExecutingAssembly());
string str1 = rmg.GetString("resxTxt"); // resxTxt is any string in your resx file.
It worked perfectly.
Keep original resources in Project A unchanged. These were already Embedded Resource.
Enjoy!
You could add a reference to the project that contains the resource file. However, to use the resource file which is generated as an internal class by default (inaccessible in your project if it's under a different namespace), you need to change the access modifier to public. See https://stackoverflow.com/a/4274341/3666333
You could create a separate project C containing the resources that both A and B can use.
Related
I have a solution with two projects:
Project A, a large private project
Project B, a small public project which needs a subset of the functions from A
I'm just getting started on Project B, but I already have a Project (A) which handles many things that will be needed in the new one. I would obviously like to reuse these functions without copying them.
If I add these as a reference in Visual Studio (2012) everything works code wise for Project B, but the generated assembly directory will also contain a functioning copy of Project A.exe.
I need to include my code from Project A without having a compiled .exe file in my output directory. Either by having it as a .dll file or inlined into the main assembly without having to refactor out the relevant parts of Project A into a new class library project. Is this possible?
Thanks for your time
I think you can try to change the Application Type of your "A Project" from Windows Application to Class Library
After that, compile your "A Project" and you will find the .dll in the output directory (instead the .exe)
Now that you have the .dll you can use it as a reference in your "B Project".
To change the Application Type you can visit this link form MSDN
In an Visual Studio environment, Project A (ASP.NET) references Project B (C#) in my solution like this:
Solution
├─Project B
│ ├─data.txt
│ └─process.cs (a class BB with static initialization new FileStream("data.txt"))
└ Project A (referencces Project B)
├─bin
│ └─data.txt (copied from project B each time project B changes)
└─Controllers
└─mycontroller.cs (references the class BB)
The problem is that when I run the application, the working directory is C:\Program Files (x86)\IIS Express\, so that data.txt cannot be read from the compiled version of process.cs which is in bin.
For the moment, to solve the problem, I manually copied data.txt to this folder, but this solution is not viable.
Note that changes to B must be coherent with other projects depending on B, which are not all ASP.NET project.
What changes should I make so that data.txt is accessible from my project without relying on me to copy the data.txt file to the IIS Express directory?
I would like to port my program to Azure Online and I cannot rely on this method. Thank you for your help.
Other linked answers:
This answer is ASP.NET - specific, I cannot add server.MapPath because the project does not know about it.
This answer references project paths, but does not give me an hint about how to modify them.
"Note that changes to B must be coherent with other projects depending on B, which are not all ASP.NET project"
Not sure what coherent would mean to you in context of other projects which depend on project B...
But following options come to mind -
Embed data.txt as a resource in the assembly generated for project B. Project B, can then read the file as a resource (This assumes, file contents do not need to be modified after the build)
See ResourceManager class for handling resources embedded in assemblies.
http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(v=vs.110).aspx
If it works for all clients of Project B, scan sub-directories for the file.. This is more hackish.. but really depends on the scenarios for project B.
Ensure that data.txt is being deployed somewhere. Then, make your library take the path to it or the base directory path as an argument. The caller of the library is the application which has concrete knowledge of how to obtain the file's path.
Or, embed the .txt into the DLL as a manged resource.
To me your problem is not about copying the file - but its location. A shared file should be kept in a shared / central location accessible by all applications. In your current situation this could be (and this isn't exhaustive)...
Database Blob
Explicit File Location
Online, retrieved over HTTP
Online, retrieved over FTP
Networked location
Running a small TCPIP server between the programs so that one acts as the source and child processes request the file.
In respect to porting your application to Azure, Azure provides a BlobStorage along with functionality to access the file in an unsecured and secured manner (see Shared Access Signatures).
HTH
Can't you just configure your project B with your project A ? I mean giving the path of your txt file to some static field of a configuration class from project A to project B.
I have a problem with resource files.
I have a solution with two projects. The first project contains ImageResource.resx file with the images that I use. Every Form in this project can access this file from the designer. But I can see in the designer ImageResource.resx file to use it from second project (Reference to second project is present).
I have added the ImageResource.resx file as a link to my second project. And I saw it in the designer! But when I use an image from this resource in the second project Visual Studio modified my original file (It sets the namespaces, and other..) and my solution breaks. Also Visual Studio tells me that ImageResource.resx is present in two dll's first_project.dll and second_project.dll
Can anybody help me with How to correctly use shared resources between projects?
The correct way to share resources is to create a global shared project. Create a new Project with the name Resources:
Next we must add some resources (such as icons) to the project. Do this as usual. Go to the projects setting, select tab Resources and Add Existing File… to the project. We can see that the icon is added to the project and was copied to the local folder:
Next step consists of adding this icon to the other project(s). Note the important difference, you need to add this icon as a link!
Adding as a link avoids the resource duplication. Create a new Project within the same solution and name it e.g. Main. Create some folder in this new project, naming it Resources (the logical name for our purpose). Then right click on this folder, select Add Existing Item… and choose the image file from the shared project folder. Make sure to use Add As Link here! If done correctly the icon of the newly added file will look slightly different (see below):
Added resource's icon must look like this
Now we must set the Build Action for this file to None. For this select the file and go to the Properties Window. There choose None for Build Action. We need to do this to avoid embedding this icon into the assembly:
Finally we need to add the linked files to the Resources of the corresponding project. Open the project Properties for the project where we just added the files. Choose the Resource tab and drag the linked file there:
These are the five simple steps you must perform to share icons between projects. You might ask "What are the benefits of this?" The benefits are:
We store resources in one place, and
It is easy to replace an icon with a new one.
This didn't work for me and I found another (VS2015+) approach.
See https://stackoverflow.com/a/45471284/4151626
In short, the shared project is directly included into the peripheral project. Thus, even though the IDE does not support <Resource> elements in the shared project. <Resource> elements can be added to the shared project, via a text editor. They are then incorporated into the peripheral project during the build.
(Apologies for the hyper-link. I would just repost the answer for clarity, but the stackoverflow editors crack down on this, deleting duplicate answers to save you from ???.)
Can you use a symbolic link to share the file into multiple folders?
windows:
mklink linked_location\ImageResource.resx original_location\ImageResource.resx
C:\Users\preet>mklink
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
If a resource file is really shared between projects, you should put it in a shared project.
Solution 'Sample'
Project Sample.Data
Project Sample.Business
Project Sample.UI
Project Sample.Resource //shared resources are put in a shared project
You can't see the resource if it is not public, and it is default set to "Friend". You can set the "Access Modifier" in the Visual Designer (upper right-hand corner).
I want to add some xml file as resource to my class library project .
Any idea how to do so , and call it later?
In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml
But here it didn't worked any idea how i do it here ?
This article explain how to use embedded resources in C#.
It boils down to
At 'Design time': Add file to project and mark it as an embedded resource (properties)
At 'Run time': Get instance of Assembly that holds the resource you want, then get that resource out as a stream.
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("fully.qualified.name.of.the.resource");
If you're struggling to work out the fq name of the resource, a lazy way is to use the reflector to open the assembly that holds it. Go to the Resources folder, right click on the particular resource and choose 'Copy'.
In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml
By default, the class generated to access the resources is internal, so you can't access it from another assembly. If you need to make it public, select the .resx file, go to its properties, and change the Custom tool property from "ResXFileCodeGenerator" to "PublicResXFileCodeGenerator". This custom tool generates public classes, which should solve your problem.
Right Click on Class Library Project, select properties, on tabs on the left choose Resources section.
Since your .resx files ares not recognizable by Visual Studio, add a new one.
It will create a new .resx file.
Copy all your original content and paste into new one. Then you can delete old one.
That's it!
You would need to repeat same process for your all supported languages.
Where do I put .resx files? Sometimes I see these files under Properties folder. Is there any design guideline about it?
Thank you!
Depends on what you are doing.
For example a web Application they go in App_GlobalResources or App_LocalResources folder.
For other projects I would create a Resource folder and put them there.
In a WinForms app, the .resx file associated ot a class (form, user-control,...) is stored side-by-side with the source code (e.g. C#) file. In addition, a global .resx file is created in the properties to let you store global stuff such as messages, pictures,...
If you right click your project in VS solution explorer you should have an option "Create folder" this will then have a list of folder you can create. App_GlobalResources and App_LocalResources is another.
For project wide strings and resources add the contents to the App_Global forlder. for form specific resources add the content to the App_Local folder instead.
Have a look here for more information