我无法使SevenZipLib库工作(p / invoke)

时间:2016-01-08 21:05:38

标签: c#

关于c#的非常基础的知识,是第一个使用p / invoke相关的东西。

请帮助我,我已经制作了这段代码,但似乎没有用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        [DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
        public static extern void SevenZipArchive(string c);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
            {
                foreach (ArchiveEntry entry in archive)
                {
                    Console.WriteLine(entry.FileName);
                }
            }
        }
    }
}

它告诉我,SevenZipArchive是一个'方法',并且被用作'类型'。

我已将库包含到我的项目中,我只是不知道如何使用它。

这是图书馆: https://sevenziplib.codeplex.com/

1 个答案:

答案 0 :(得分:2)

首先需要删除此代码:

[DllImport("...")]
public static extern void SevenZipArchive(string c);

您不需要提供任何p / invoke声明。图书馆为你解决了这个问题。

这是一个.net程序集。你就像使用任何其他东西一样使用它。请执行以下步骤:

  1. 下载项目。你已经这样做了。
  2. 构建解决方案。确保
  3. 在项目中,添加对在上一阶段中构建的程序集的引用。它是SevenZipLib\bin\Debug\SevenZipLib.dllSevenZipLib\bin\Release\SevenZipLib.dll,具体取决于您选择的目标。
  4. using SevenZipLib;添加到项目代码中以获取对命名空间的访问权。
  5. 完成后,您的代码就可以运行了。您可以使用作为下载的一部分提供的测试项目作为示例代码的丰富源。