简体   繁体   English

如何在具有不同名称空间和dll的C#脚本中使用外部库

[英]How to use external library in c# script having different namespace and dll

I want to use novacode-docx in cs-script. 我想在CS脚本中使用novacode-docx。 how can I give correct reference to the assembly. 我如何才能正确地参考组装。 I tried following but didn't work around the assembly reference missing. 我尝试了以下操作,但没有解决缺少程序集引用的问题。

//css_reference D:\lib\DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        using (DocX doc = DocX.Create(@"C:\Users\name\Desktop\test.docx"))
        {
             doc.PageLayout.Orientation = Orientation.Landscape;
             var table = doc.AddTable(12, 2); 
             doc.InsertTable(table);
             doc.Save();
        }
    }
}

You cannot reference an explicit path like that for presumably security reasons. 出于安全考虑,您不能引用类似的显式路径。 The assembly must be placed in one of the following locations and referenced as //css_reference DocX.dll; 程序集必须放置在以下位置之一,并引用为//css_reference DocX.dll;

File location The assembly to be loaded must be from one of the following locations (the order indicates the assembly search priority): 文件位置要加载的程序集必须来自以下位置之一(顺序指示程序集搜索优先级):

  • the same directory where the script is 脚本所在的目录
  • Default Script Library directory Script Library (%CSSCRIPT_DIR%\\Lib) 默认脚本库目录脚本库(%CSSCRIPT_DIR%\\ Lib)
  • Custom Script Library directory(s) (specified in the configuration console SearchDirs) 自定义脚本库目录(在配置控制台SearchDirs中指定)
  • GAC GAC

See here for more info: http://www.csscript.net/help/using_.net_assemblies.html 请参阅此处以获取更多信息: http : //www.csscript.net/help/using_.net_assemblies.html

Drop the Docx.dll into the same folder as where the cs script is and try this: 将Docx.dll拖放到与CS脚本所在的文件夹相同的文件夹中,然后尝试以下操作:

//css_reference DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Novacode;

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        using (DocX doc = DocX.Create(@"C:\Users\name\Desktop\test.docx"))
        {
             doc.PageLayout.Orientation = Orientation.Landscape;
             var table = doc.AddTable(12, 2); 
             doc.InsertTable(table);
             doc.Save();
        }
    }
}

Have you read this link 你读过这个链接了

To add a reference in Visual C# In Solution Explorer, right-click the project node and click Add Reference. 在Visual C#中添加引用在解决方案资源管理器中,右键单击项目节点,然后单击“添加引用”。 In the Add Reference dialog box, select the tab indicating the type of component you want to reference. 在“添加引用”对话框中,选择指示您要引用的组件类型的选项卡。 Select the components you want to reference, and then click OK. 选择要引用的组件,然后单击“确定”。

Without VS: 没有VS:

Go to the csproj file there is a <ItemGroup> where references can be added: 转到csproj文件,有一个<ItemGroup> ,可以在其中添加引用:

<ItemGroup>
    <Content Include="libs\...">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
...

There you can add libs. 在那里您可以添加库。

DocX seems to be available on NuGet , so I would heavily recommend fetching the dependency from there rather than having it in a file on your local system. DocX似乎可以在NuGet上使用 ,因此我强烈建议从那里获取依赖关系,而不是将其保存在本地系统的文件中。 (This helps ensuring repeatable builds, should you share this code with others, with packaging your application, and it will also make it easier to upgrade DocX if a new version is released.) (这有助于确保可重复的构建,如果您要与其他人共享此代码以及打包应用程序的话,也可以使发布新版本的DocX升级变得更加容易。)

If you're using Visual Studio, you can right-click the project in Solution Explorer and choose "Manage NuGet Packages..." to open a dialog that helps you install the package, or you can open Package Manager Console and enter Install-Package DocX . 如果您使用的是Visual Studio,则可以在解决方案资源管理器中右键单击该项目,然后选择“管理NuGet软件包...”以打开一个对话框来帮助您安装该软件包,也可以打开“软件包管理器控制台”并输入Install-Package DocX

If you're building on .NET Core without Visual Studio, just add "DocX": "1.0.0.19" to the dependencies node of your project.json. 如果要在不带Visual Studio的.NET Core上进行构建,则只需将"DocX": "1.0.0.19"到project.json的dependencies节点。

When the package is installed, you can just do using DocX; 安装该软件包后,您就可以using DocX; like with any other namespace import. 就像任何其他名称空间导入一样。

both required in order to use docx. 两者都是使用docx所必需的。

//css_reference DocX.dll;
using Novacode;

You can also give reference to any place like 您也可以参考任何地方,例如

//css_reference D:\lib\DocX.dll;
using Novacode;

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

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