首页 > 解决方案 > C# 控制台应用程序需要有关主要参数错误的帮助

问题描述

我有以下 c# Console 应用程序,我将在 ssis 中运行它,但我使用了几个 PDF 操作库。所以我要在传递文件路径时从我的 ssis 包中调用一个 exe。

但是尝试通过exe运行时出现以下错误。

未处理的异常:System.IndexOutOfRangeException:索引超出了数组的范围。在 ConsoleApp.program.Main(String[] args) 第 87 行

但是如果我在调试中运行它工作正常。一旦我通过exe让它自己工作,我想将文件路径作为参数传递给ssis。

见下面的c#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using org.apache.pdfbox.pdmodel;
    using org.apache.pdfbox.util;
    using System.IO;

    namespace PDF_Read_ConsoleApp
    {
        class Program
        {       
            public static void FilePath(string path)
            {

                //Console.WriteLine("Please enter full pdf path \n\n ");

                //path = Console.ReadLine();

                string fp;

                fp = @path;

                string[] files = Directory.GetFiles(path, "*.pdf");

                foreach (string s in files)    
                {

                    string txtOutput = s.Replace(".pdf", ".txt");    
                    if (File.Exists(txtOutput))
                    {
                        File.Delete(txtOutput);
                    }

                    string output;   

                    PDDocument doc = null;

                    try
                    {
                        doc = PDDocument.load(s);
                        PDFTextStripper stripper = new PDFTextStripper();
                        stripper.getText(doc);

                        output = stripper.getText(doc);

                        StreamWriter NewFile;
                        NewFile = new StreamWriter(txtOutput);

                        //NewFile.Write(output.ToString());
                        NewFile.Write(output.ToString());
                        NewFile.Close();      

                    }
                    finally
                    {
                        //if (doc != null)
                        //{
                        doc.close();

                        //    Console.WriteLine("\n\n File saveed - ({0} ", txtOutput);

                        //}
                    }
                }
            }

            static void Main(string[] args)
            {    
                args[0] = @"C:\SSIS_Packages\PDF_Import\PDF_Import\PO_pdfs";   ////   TESTING FILE PATH1

                FilePath(args[0]);    

            }
        }
    }

亲切的问候

标签: c#console-application

解决方案


I have managed to get it working, I need to enter an argument within the debug screen, see information in URL below

Console app arguments, how arguments are passed to Main method

THank you for everyone's comments


推荐阅读