C#控制台应用程序需要帮助处理主参数错误

C#控制台应用程序需要帮助处理主参数错误,c#,console-application,C#,Console Application,我有下面的c#控制台应用程序,我会在ssis中运行它,但我使用的是一些PDF操作库。因此,我将在传递文件路径时从ssis包中调用exe 但是我在尝试通过exe运行时遇到以下错误 未处理的异常:System.IndexOutOfRangeException:索引为 在数组的边界之外。在控制台app.program.Main(字符串[]) args)第87行 但如果我在debug中运行,它就可以正常工作。一旦我通过exe让它自己工作,我想在ssis中将文件路径作为参数传递 见下文c 问候 Rob我已

我有下面的c#控制台应用程序,我会在ssis中运行它,但我使用的是一些PDF操作库。因此,我将在传递文件路径时从ssis包中调用exe

但是我在尝试通过exe运行时遇到以下错误

未处理的异常:System.IndexOutOfRangeException:索引为 在数组的边界之外。在控制台app.program.Main(字符串[]) args)第87行

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

见下文c

问候


Rob

我已经设法让它工作,我需要在调试屏幕中输入一个参数,请参阅下面URL中的信息


感谢大家的评论

检查参数的计数,它可能不是您期望的值为什么要在主参数中填充参数?只需使用一个变量。从Main method initialized中的cmdis args调用程序时使用的arg?我认为我们无法帮助您,因为问题似乎在于如何调用应用程序。我只能告诉你许多调试想法中的一个。添加对
Console.WriteLine($“有{args.Length}个参数”)的调用FilePath()
之前,在Main方法中使用code>。这将打印应用程序收到的参数数量。如果值为0,您将获得异常,但是如果值高于此值,则不应获得
System.IndexOutOfRangeException
。现在,您可以在任何地方调用它,并检查该值是否高于0。如果不是,则说明您没有正确调用它。
    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]);    

            }
        }
    }