sharing objects between .net applications

G

Gregory Khra

I need to reuse the object from one .net application in another .net
application. So far I was able to do it as long as both .exe files were in
the same folder. How can I place them in different folders and make sure
that one application finds another at runtime?
 
S

Stanimir Stoyanov

Do you mean you want to reuse code from a dependency library? If so, you can
distribute the library with both of the executables or install it in the GAC
(Global Assembly Cache). That way it will always be found but versioning
might become a slight problem.

Search for "assemblies GAC" and you should be able to find the necessary
information.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info
 
G

Gregory Khra

Stanimir Stoyanov said:
Do you mean you want to reuse code from a dependency library?

No, this is exactly what I am trying to avoid. It would be difficult to
extract necessary objects from the main application into an independant DLL.
I just want to reuse one class from the main application in a different app.
with minimum work. I don't want to install the main app to GAC (I am not
even sure I can install .exe to GAC). Is there a way to tell auxiliary app
where the main app is?
 
S

Stanimir Stoyanov

You can add a shortcut to your .cs file in another project from Project->Add
Existing Item... and make sure to select Add As Link. Changes to the code
will reflect in both projects. Note that you might have to add other classes
in the same fashion in case the first one references them.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info
 
G

Gregory Khra

You can add a shortcut to your .cs file in another project from Project->Add
Existing Item... and make sure to select Add As Link.

Unfortunately this .cs file uses objects that are defined in the other .cs
files which are using other objects ... etc. As I said, it is difficult to
extract the object I need from its environment (I realize that it's bad
design, but it's too late to change).

So far I was able to create a reference in my Visual Studio project and just
reuse the namespace from another application. The problem is that it only
works if at runtime both exe files are in the same folder.
 
S

Stanimir Stoyanov

I see. To answer your question a post above "Is there a way to tell
auxiliary app
where the main app is?", I do not know what kind of mapping your application
is using but it *might* be possible to achieve this by changing the current
directory of the execution context at application startup, preferably as the
first call in your Program class:

Environment.CurrentDirectory = @"C:\my\path";

You still have to be able to determine what the path should be.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info
 
G

Gregory Khra

Environment.CurrentDirectory = @"C:\my\path";

I tried it. Doesn't work. Now I am playing with app.config but cannot find
the appropriate syntax. I was hoping someone can give me an example.
Gregory
 
J

Joern Schou-Rode

I tried it. Doesn't work. Now I am playing with app.config but cannot
find
the appropriate syntax. I was hoping someone can give me an example.
Gregory

The app.config has an element where you can tell the runtime environment
where to look for assemblies. However, the paths that you add here can
only be "private" - ie. subdirectories to the installation dir of the
client app. More info on this option here:
http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

If this does not work for you (from what you have told I guess it does
not), I believe that your only option is to do an explicit call to the
Assembly.LoadFrom() method, documented here:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top