简体   繁体   English

csc.exe参考外部.dll文件

[英]csc.exe reference external .dll file

I am trying to make a simple c# program using Growl C# API . 我正在尝试使用Growl C# API制作一个简单的c#程序。

I tried to compile my program in two different ways: 我尝试用两种不同的方式来编译程序:

1) I kept my .dll file in the same directory as my .cs file. 1)我将.dll文件保存在与.cs文件相同的目录中。 Than I ran 比我跑

csc /r:Growl.Connector.dll,Growl.CoreLibrary.dll /out:test.exe *.cs

It compiled fine and also ran fine. 它编译良好,也运行良好。

2) Now I have created a directory inside my current working directory named growl and kept all my .dll references there. 2)现在,我在当前的工作目录中创建了一个名为growl目录,并将所有.dll引用保存在该目录中。

Now when I try to compile it using the below command 现在,当我尝试使用以下命令进行编译时

csc /r:"D:\Modified\Growl_NET_Connector_SDK\libraries\growl\Growl.Connector.dll","D:
\Modified\Growl_NET_Connector_SDK\libraries\growl\Growl.CoreLibrary.dll" /out:test.exe *.cs

It compiled fine but when I tried to run it the below mentioned exception occurred. 它编译良好,但是当我尝试运行它时,发生了以下提到的异常。

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Growl.Connector, Version=2.0.0.0, Culture=n
eutral, PublicKeyToken=980c2339411be384' or one of its dependencies. The system cannot find the file specified.
 at GrowlNotification.Program.Main(String[] args)

So, my question is what is the correct way to reference .dll file in csc when files are in an external folder. 因此,我的问题是,当文件位于外部文件夹中时,在csc引用.dll文件的正确方法是什么。

Here is the directory structure for 2nd case. 这是第二种情况的目录结构

So, my question is what is the correct way to reference .dll file in csc when files are in an external folder. 因此,我的问题是,当文件位于外部文件夹中时,在csc中引用.dll文件的正确方法是什么。

You're already referencing them at build time. 您已经在构建时引用了它们。 You just need to make them available at execution time too, but copying them into the same directory as the executable, when you want to run it. 您也只需在执行时使它们可用,但是当您想运行它们时,将它们复制到与可执行文件相同的目录中。

You could also investigate using the Global Assembly Cache if these are signed assemblies, but personally I'd stick with just keeping the executable with the libraries on which it depends. 如果这些程序集是经过签名的程序集,您还可以使用Global Assembly Cache进行调查,但就我个人而言,我坚持将可执行文件保留在其依赖的库中。

You can add these using the /lib and /reference command-line switches while compiling. 您可以在编译时使用/ lib和/ reference命令行开关添加它们。

http://msdn.microsoft.com/en-us/library/s5bac5fx.aspx http://msdn.microsoft.com/en-us/library/s5bac5fx.aspx

But (Quote from the article) 但是 (本文引述)

An alternative to using /lib is to copy into the working directory any required assemblies; 使用/ lib的一种替代方法是将任何所需的程序集复制到工作目录中。 this will allow you to simply pass the assembly name to /reference. 这将使您可以简单地将程序集名称传递给/ reference。 You can then delete the assemblies from the working directory. 然后,您可以从工作目录中删除程序集。 Since the path to the dependent assembly is not specified in the assembly manifest, the application can be started on the target computer and will find and use the assembly in the global assembly cache. 由于未在程序集清单中指定从属程序集的路径,因此可以在目标计算机上启动该应用程序,并且该应用程序将在全局程序集缓存中查找并使用该程序集。

Because the compiler can reference the assembly does not imply the common language runtime will be able to find and load the assembly at runtime. 因为编译器可以引用程序集,但这并不意味着公共语言运行时将能够在运行时查找并加载程序集。 See How the Runtime Locates Assemblies for details on how the runtime searches for referenced assemblies. 有关运行时如何搜索引用的程序集的详细信息,请参见运行时如何查找程序集。

so Jon Skeet's answer is better. 所以乔恩·斯基特的答案更好。 (I'm just adding this to provide more info than I could in a comment, not as an answer. Jon's answer is the best IMO) (我只是添加此内容以提供比评论中更多的信息,而不是作为答案。乔恩的答案是最好的IMO)

您可以在库文件夹中创建到程序集的符号链接 ,因此只需要在一个位置保持它们的更新即可。

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

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