I have a class library that uses some xml files found in its own directory.
When referencing this library from another project, how do I ensure the library is working from it's own directory?
I tried Assembly.GetExecutingAssembly().Location but that still returns the path of the startup project.
I know this is an old post but in the event someone else stumbles on it from a search, the easiest way I've found to do this is:
Public Class Foo
Public Shared Function GetMyPath() As String
Return Path.GetDirectoryName(GetType(Foo).Assembly.Location)
End Function
End Class
This is happening because your startup project has two options as to loading the library
Copy it to it's own folder (what would cause the behaviour you are experimenting)
Load it from GAC (what would make impossible to load any XML from it's folder)
So you have two options here:
Import the XML files in the project and embed them as "embedded resources" in the class library and load it in memory at runtime
Import the XML files in the project and change the "copy to output directory" property of the file to "true".
I don't think you can unless you start it in a new AppDomain, and that's more trouble than it's worth. Just copy the files it needs into your own project, and put them in the place it expects relative to your working directory.
(You might also want to ask the original developer why the paths to the needed files can't be given as a parameter. This would be the normal way of solving this problem.)
I'm sure there is some functions to get the list of loaded assemblies from the ExecutingAssembly, loop across this to find you assemble(class lib) then get it's location. Sorry for vague answer.
Related
When I am trying to call a method of another project class file, I am getting the following error
I have 2 C# library projects inside same solution file.
Inside each project I have a class file.
I have added reference of one project inside other.
There are NO compile time or build errors.
During runtime, it is throwing the following exception (Image attached above)
My question is, how does one able to call a class method of one project from another method ?
Thanks in advance :)
Is your current project class file exist in the actual directory? The physical file might not be on the disk itself so it has this error message.
Simply put if your physical file exist in your directory, and if you wanna call that method in, be sure to put the namespace at the top and import that reference from your solution explorer.
Also, Do remember to include your method class as public.
Example Below:
public class GetUserDetails
{
//methods
}
First check the target framework and verify that the two C# library projects have the same framework.
Did you miss deploying or referencing any assembly (DatabaseUtil.dll) in the old version?
Check to make sure that the referenced assembly exists in your bin folder. If it does exist, check if it is 32-bit or 64-bit.
Go to References in Solution Explorer in Visual Studio. Select the assembly which is getting complained.
Set its Copy Local to true in its Properties page.
And I suggest you try the suggestions in this thread.
If the above methods are not successful, please provide the relevant code to reproduce the problem.
Daniel Zhang
I'm new in .Net and working with two projects in c# a class library project(dll) and a website asp project.
I need to read some properties from a file .resx that is in the App_GlobalResources folder of the website.
Is there a way to read these properties in the .resx website file from a dll assembly more specifically in the method onPreRender??
Thanks for you attention
It sounds to me that you are having some problems with structuring and dependencies in your solution. (Trying to reference the website from a DLL)
Generally speaking, your DLL should not need to access the resources of the website on its own - you should only pass them in through as parameters when calling various methods that are contained in the DLL itself.
Have you thought about migrating the resource file to the DLL?
That would allow both DLL and the website to read from it.
Another option would be to migrate the setting you need to the .config file which you can read by using the ConfigurationManager class ( MSND Link )
You should be able to use it like this, even from your Code repository project:
string settingValue = ConfigurationManager.AppSettings["YouSettingNameHere"].ToString();
However, if you really want to keep your current solution structure you can follow the answer that Pavel Chuchuva gave on a similar question here.
I have a Visual Studio 2010 C# project which creates an .exe and this project is using some 3rd party class library.
My project is located in: /MyFramWork/tests/test1
3rd party library is located at: /MyFrameWork/bin/utils/
I am adding the reference to the library by using References->Add Reference->Browse. I can see that in the project file all is fine:
....\bin\utils\log4net.dll
False
I would like to reference the 3rd party library without using the option "Copy Local". However if I don't use the option, the library is not found and I get an exception.
My question is: Is there a way to specify that the 3rd party library should be found at ....\bin\utils. It seems that when the .exe gets build the information from the .csproj gets lost.
By default, .NET apps look for their dependencies in only two places: the EXE directory, and the GAC (Global Assembly Cache).
You have three choices:
You can make sure the dependency gets copied into the same directory as your EXE (this is what Copy Local does). This is the best choice most of the time, which is why it's the default when you reference an assembly that's not already in the GAC.
You can install your dependency into the GAC using gacutil. This might be a good choice if your dependency isn't going to change, is going to be in a different location on every development machine (i.e. if relative paths won't work well), and if you want to use it from many different projects. But it's a major pain if the dependency is still under active development and changing frequently. You'll also need to make sure to put the DLL into the GAC on every computer you deploy your app to.
You can customize the dependency-loading behavior so it looks in other places, as Hans noted in his comment. This is an advanced option and comes with a whole new set of headaches.
Normally, you would just use Copy Local; it's a very sensible default. You should need a fairly compelling reason to do anything different.
Use the <probing> element to specify where the CLR should search for your assemblies. The only restriction is that the assemblies must be located in subdirectories of your application's base directory.
For example, if your application base directory is C:\MyFramework, then you could have your assemblies in C:\MyFramework\bin.
Have a look at this article to learn how the CLR searches for assemblies.
If you need to load assemblies from custom locations, you could try the Assembly.LoadFile Method.
The following links may be useful:
C# - Correct Way to Load Assembly, Find Class and Call Run() Method
http://www.csharp-examples.net/reflection-examples/
It's me Potzon. I am still investigating this incredibly silly problem.
I have been hoping for some elegant solution. I am about to build fairly large framework with lots of assemblies which would be placed inside /Framework/bin/. However I wanted to have some directory structure inside the the directory, for example /bin/utils, /bin/test, /bin/devices/ and so on.
One possible solution that I have found is to define environmental variable DEVPATH (see here http://msdn.microsoft.com/en-us/library/cskzh7h6.aspx) but it turns out that .net4 is not using this variable when an assembly is run independently (outside the visual studio), or at least this is the case for me - I can't make it work.
It seems that the solution to put all the assemblies inside the /bin directory without using sub-directories is the best. I think I will give up and just do it this way.
I want to move the localized resource files, created for my various winforms, to a resource dll. Is there an easy way to do it, that don't include manually reading every single string from the dll for each form, just like the forms usually handles it?
I don't think it'll be that difficult, you just have to create a new project of type class library and move your resource file to that project, when you build that project it'll create the dll and then you just need to refer this dll to your actual project and done.
I have a class library project that i have made. Let's call it ClassA. In ClassA i need to access some tools that reside in dll (ToolsDLL.dll).
In ClassA I have added ToolsDLL.dll to the project and selected the ToolsDLL.dll file to Copy To Output directory ALWAYS. So that library builds and compiles just fine and in the output directory i see ClassA.dll along with ToolsDLL.dll
Next, I want to write an application, say App_A that uses the methods in ClassA. So, in my App_A project, I added a reference to ClassA.dll so that I can access it's namespace. All is well and good, it build/compiles.
The problem is as soon as I run App_A and it gets to a point where ToolsDLL.dll needs to be used it throws an exception "Unable to Load ToolsDLL.dll. I don't understand how it is possible that it can't find that dll because it is in the same directory as ClassA.dll.
I found that if i put ToolsDLL.dll in the output directory of App_A it works just fine. Is there any way around that? Is there any way that ToolsDll.dll can be somehow bundled with ClassA.dll. The reason is that my customers will be writing their own applications similar to AppA and it would be nice if they only had to reference one file in their project and not multiple.
There's a tool from Microsoft called ILMerge. It will probably do what you want, bundling several assemblies into one file.
P.S.: Another, fairly frequently used solution to your problem would be to add a post-build event to your application's solution/project that copies the required ToolsDLL.dll over to the output directory? Something along the line of:
xcopy /y /d $(SolutionDir)\lib\ToolsDLL.dll $(OutputDir)\ToolsDLL.dll
(Sorry if I get some of it wrong, I'm typing this from my memory.)
Of course, your customer would also have to do this. But then again they've probably done this before.
Have you added the DLL to the project, or have you actually added a reference to it? You should do the latter, then this sort of thing is taken care of automatically for you. It sounds like you have added the actual file to the project files, and set it to copy.
If you do definitely want a single file approach, then accept the others' suggestions of ILMerge obviously...
You can use ILMerge to combine assemblies into one DLL.
https://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
You can either use an IL merging tool, or install ToolsDLL.dll in the GAC.
Also if you look at the output of App_A, ClassA.dll is already there, that's where it's looking for ToolsDLL.dll.