iTextSharp的System.IO.FileNotFoundException

问题描述:

我正在上的下面的发布版本一个System.IO.FileNotFoundException,但没有得到它,而调试。iTextSharp的System.IO.FileNotFoundException

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 
using Microsoft.Office.Interop.Word; 
using System.IO; 
using iTextSharp.text.pdf; 
using iTextSharp.text.pdf.parser; 
using iTextSharp.text.xml; 
using Microsoft.VisualBasic; 

namespace CountingTool 
{ 
    public partial class MainWindow : System.Windows.Window 
    { 
     private string SelectedFile; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     }   

     private void LoadFileButton_Click(object sender, System.EventArgs e) 
     { 
      OpenFileDialog LoadFileDialog = new OpenFileDialog(); 

      LoadFileDialog.Title = "Select file"; 
      LoadFileDialog.Filter = "PDF files|*.pdf"; 
      LoadFileDialog.InitialDirectory = @"C:\"; 

      if (LoadFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       try 
       { 
        SelectedFile = LoadFileDialog.FileName; 
       } 
       catch (Exception ex) 
       { 
        System.Windows.Forms.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
       } 
      } 
     } 

     private void CountEach() 
     { 
      string FullPdf = ExtractFullPdf(); 
      ... 
     } 

     private string ExtractFullPdf() 
     { 
      PdfReader sourceDocument = new PdfReader(SelectedFile); 
      StringBuilder text = new StringBuilder(); 

      for (int i = 1; i <= sourceDocument.NumberOfPages; i++) 
      { 
       text.Append(PdfTextExtractor.GetTextFromPage(sourceDocument, i)); 
      } 

      sourceDocument.Close(); 
      return text.ToString(); 
     } 
} 

似乎是导致问题的线路是PdfReader sourceDocument = new PdfReader(SelectedFile);

任何想法将不胜感激。

感谢

+0

当您运行发布vs测试版本时,您是否在相同的环境中运行您的可执行文件? –

+0

@MattSpinks是的,我是。 build/bin/Debug可以正常工作,但/bin/Debug/app.publish中的内部版本有问题。 – stutch

+0

我只是想指出你的try catch块在一个字符串到字符串赋值期间正在防范出错的地方。 –

问题是,itextsharp.dll需要在同一目录中内置.exe文件。

如果你完全确定你所指定的路径是正确的,尽管这可能是不可能的话,我可以建议您使用:

Request.PhysicalApplicationPath(“在此处输入路径“);

使用Server.Mappath( “在此处输入路径”);这里