Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top