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

本文关键字:invoke 工作 SevenZipLib | 更新日期: 2023-09-27 18:29:36

关于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/

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

您首先需要删除以下代码:

[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;添加到您的项目代码中,以获得对命名空间的访问权限

一旦你做到了,你的代码就会工作。您可以使用作为下载的一部分提供的测试项目作为示例代码的丰富来源。