You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForm发布后Tesseract OCR抛出ArgumentNullException求助

ClickOnce发布后Tesseract OCR抛出ArgumentNullException(path1参数为空)

程序在Visual Studio中运行正常,但通过ClickOnce发布后持续抛出以下错误:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.ArgumentNullException.Throw(String paramName)
   at System.IO.Path.Combine(String path1, String path2)
   at InteropDotNet.LibraryLoader.InternalLoadLibrary(String baseDirectory, String platformName, String fileName)
   at InteropDotNet.LibraryLoader.LoadLibrary(String fileName, String platformName)
   at InteropRuntimeImplementer.LeptonicaApiSignaturesInstance.LeptonicaApiSignaturesImplementation..ctor(LibraryLoader loader)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr)

核心业务方法代码片段

private void SearchAndCopyPDFsWithOCR(
string sourceDirectory,
string targetDirectory,
string searchTerm,
DataTable dataTableValues,
string partnerPrefix,
string wetenr)
{
    string tessdataPath = @"C:\\Program Files\\Tesseract-OCR\\tessdata";

    if (!Directory.Exists(tessdataPath))
    {
        MessageBox.Show($"A tessdata mappa {tessdataPath} nem található!", "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    if (string.IsNullOrEmpty(searchTerm) || string.IsNullOrEmpty(partnerPrefix))
        return;

    var filteredDirectories = Directory.GetDirectories(sourceDirectory, $"{partnerPrefix}*");

    // A TesseractEngine csak egyszer jön létre a metódus elején.
    using (var ocrEngine = new TesseractEngine(tessdataPath, "eng", EngineMode.LstmOnly))
    {
        // 后续OCR处理逻辑
    }
}

已尝试的排查措施

  • 日志所有Path.Combine参数,路径显示正常
  • 添加异常处理逻辑,未触发
  • 检查所有传入方法的参数,确认非空
  • 使用C盘预安装的Tesseract目录(硬编码路径)
  • 更换本地桌面作为源路径

调用OCR方法的代码片段

// Mappa ellenőrzése és PDF keresés
if (sourceDirectory == null || evszam == null)
{
    MessageBox.Show($"sourceDirectory vagy evszam null {sourceDirectory}, {evszam}");
}
else
{
    if (sourceDirectory == null || Path.GetFileName(evszam) == null)
    {
        throw new ArgumentNullException($"{sourceDirectory} vagy {Path.GetFileName(evszam)} nem lehet null.");
    }
    specificSourceDirectory = Path.Combine(sourceDirectory, evszam);

    MessageBox.Show(specificSourceDirectory);
}

if (Directory.Exists(specificSourceDirectory))
{
    SearchAndCopyPDFsWithOCR(
        specificSourceDirectory,
        targetDirectory,
        szamlaszam,
        dataTableValues,
        partnerPrefix,
        wetenr);
}
else
{
    SearchAndCopyPDFsWithOCR(
        sourceDirectory,
        targetDirectory,
        szamlaszam,
        dataTableValues,
        partnerPrefix,
        wetenr);
}

求助需求

目前所有尝试均未解决问题,恳请帮忙排查遗漏的问题点。

内容的提问来源于stack exchange,提问作者Bálint

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 15:49:58