0

I have added a .NET dll library "itextsharp" that I am using in my C# code. The dll can be present in the following directories:

C:\ProgramData\<Application Name>\ or
C:\Users\<Username>\AppData\
C:\Windows\System32\ or
C:\Windows\

I want to know how to write a code which checks if the dll is present in any of the above location and then uses it. like:

string sPath = "";
if(File.Exist(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\<Application Name>"\\itextsharp.dll"))
  sPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\<Application Name>"\\itextsharp.dll");
else if(File.Exist(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\<Application Name>"\\itextsharp.dll"))
  sPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\<Application Name>"\\itextsharp.dll");

.... and so on.....

and then how to use the sPath variable to access the library and its namespace and classes.

I want it this way because the file is not always in a specific folder and I do not want to keep all code related files in the main Program files folder: "C:\Program Files\<Application Name>\" If the file already present in any of the above location then prefer to use that first. So if I add a .NET library using "add reference", then is it a fixed path? I need to first find the correct path and then instruct code to use it and access it namespace and classes.

My second question is that if I add a .NET dll from a location using "add reference", then should the path be exactly same when it is deployed on client's machine? For example if my windows drive is C: and I use path "C:\Users\<Username>\AppData\itextsharp.dll" but if client has Windows installed in D: drive, then will the code access path from C drive or D drive? And how to change the path programmatically?

Computer User
  • 2,839
  • 4
  • 47
  • 69
  • 2
    Well, you can register a dll in GAC (Global Assemblies Cache) or referer to it using a relative path. For example, you can create, in your project a folder called "Libs" in which you can store all your external libraries necessary to work. When you execute the deploy these libraries will be always deployed with your project (in bin folder, no differences between web and client apps). – Roberto Conte Rosito Apr 15 '14 at 14:55
  • 1
    Perhaps this will help: http://stackoverflow.com/questions/15666618/csproj-multiple-hint-paths-for-an-assembly – Michael Gunter Apr 15 '14 at 14:55
  • 1
    Aren't you just trying to solve a Fusion problem? http://stackoverflow.com/questions/9961162/what-is-fusion-in-net-assembly – rene Apr 15 '14 at 14:57

1 Answers1

1

if I add a .NET library using "add reference", then is it a fixed path? - There is no path involved while working with assemblies/binaries in project.

Here is the practice steps you can follow,

  1. In root folder of your project, there should be binaries/dependencies folder, where you'll store path-less(which is either not installed in your workstaion or third party like iTextSharp,telerik) assemblies.
  2. You will reference required assemblies from this project's folder only.

  3. When you deploy/publish, make sure you set copy Local option set to True. Select assembly from References tab, then navigate to properties - You are telling visual studio to copy all these referenced assemblies to bin directory of your published/deployed directory.

if I add a .NET dll from a location using "add reference", then should the path be exactly same when it is deployed on client's machine? - Above practice will eliminate such possibilities.

Whether project is installation or web, its published copy must contain all the dependencies so we call it a package.

Palak Bhansali
  • 731
  • 4
  • 10