C# csc.exe 引用外部 .dll 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10722832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
csc.exe reference external .dll file
提问by RanRag
I am trying to make a simple c#program using Growl C# API.
我正在尝试c#使用Growl C# API.
I tried to compile my program in two different ways:
我尝试以两种不同的方式编译我的程序:
1)I kept my .dllfile in the same directory as my .csfile. Than I ran
1)我将.dll文件保存在与文件相同的目录中.cs。比我跑
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 growland kept all my .dllreferences 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 .dllfile in cscwhen files are in an external folder.
所以,我的问题是当文件位于外部文件夹.dll中csc时引用文件的正确方法是什么。
Here is the directory structurefor 2nd case.
这是第二种情况的目录结构。
采纳答案by Jon Skeet
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 buildtime. You just need to make them available at executiontime 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.
如果这些是签名的程序集,您也可以使用全局程序集缓存进行调查,但我个人坚持只将可执行文件与其依赖的库一起保存。
回答by David
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; this will allow you to simply pass the assembly name to /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.
使用 /lib 的替代方法是将任何需要的程序集复制到工作目录中;这将允许您简单地将程序集名称传递给 /reference。然后您可以从工作目录中删除程序集。由于程序集清单中未指定依赖程序集的路径,因此可以在目标计算机上启动应用程序,并将在全局程序集缓存中查找和使用该程序集。
因为编译器可以引用程序集并不意味着公共语言运行时将能够在运行时找到并加载程序集。有关运行时如何搜索引用程序集的详细信息,请参阅运行时如何定位程序集。
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)

