简体   繁体   中英

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

I want to use novacode-docx in cs-script. 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;

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)
  • Custom Script Library directory(s) (specified in the configuration console SearchDirs)
  • GAC

See here for more info: 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:

//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. 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:

Go to the csproj file there is a <ItemGroup> where references can be added:

<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. (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.)

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 .

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.

When the package is installed, you can just do using DocX; like with any other namespace import.

both required in order to use docx.

//css_reference DocX.dll;
using Novacode;

You can also give reference to any place like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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