简体   繁体   English

从不同位置在.NET中添加相同的dll引用

[英]Add same dll reference in .NET from different locations

I have added a .NET dll library "itextsharp" that I am using in my C# code. 我已经在C#代码中添加了一个.NET dll库“ itextsharp”。 The dll can be present in the following directories: dll可以存在于以下目录中:

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. 我想知道如何编写代码来检查dll是否存在于上述任何位置,然后使用它。 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. 然后如何使用sPath变量访问库及其名称空间和类。

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. 我想要这种方式,因为该文件并不总是位于特定的文件夹中,并且我不想将所有与代码相关的文件保留在主Program Files文件夹中: "C:\\Program Files\\<Application Name>\\"如果该文件已经存在)存在于上述任何位置的用户,则更喜欢先使用该位置。 So if I add a .NET library using "add reference", then is it a fixed path? 因此,如果我使用“添加引用”添加.NET库,那么它是固定路径吗? 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? 我的第二个问题是,如果我使用“添加引用”从某个位置添加一个.NET dll,那么将其部署在客户端计算机上时,路径应该完全相同吗? 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? 例如,如果我的Windows驱动器是C :,并且我使用路径"C:\\Users\\<Username>\\AppData\\itextsharp.dll"但是如果客户端在D:驱动器中安装了Windows,则将从C驱动器或D盘? And how to change the path programmatically? 以及如何以编程方式更改路径?

if I add a .NET library using "add reference", then is it a fixed path? 如果我使用“添加引用”添加.NET库,那么它是固定路径吗? - 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. 在项目的根文件夹中,应该有binaries / dependencies文件夹,您将在其中存储无路径(没有安装在工作区或第三方(如iTextSharp,telerik)的程序集)中。
  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 . 部署/发布时,请确保将copy Local选项设置为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. 从“ 引用”选项卡中选择程序集,然后导航到属性-您正在告诉Visual Studio将所有这些引用的程序集复制到已发布/已部署目录的bin目录中。

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? 如果我使用“添加引用”从某个位置添加.NET dll,那么在客户端计算机上部署该路径时,路径应该完全相同吗? - 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. 无论是安装项目还是Web项目,其发布的副本都必须包含所有依赖项,因此我们将其称为包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM