【问题标题】:C# Console app need assistance with main args errorC# 控制台应用程序需要有关主要参数错误的帮助
【发布时间】:2020-03-28 05:34:31
【问题描述】:

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

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

未处理的异常:System.IndexOutOfRangeException:索引是 在数组的范围之外。在 ConsoleApp.program.Main(字符串 [] 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]);    

            }
        }
    }

亲切的问候

罗伯

【问题讨论】:

  • 查看参数个数,可能不是你预期的值
  • 你为什么要在 main 中填充 arg?只需使用一个变量。从 cmd 调用程序时使用的 arg
  • Main 方法中的 args 是否已初始化?
  • 我认为我们无法帮助您,因为问题似乎与您如何调用应用程序有关。我只能告诉你许多调试想法之一。在调用 FilePath() 之前,在 Main 方法中添加对 Console.WriteLine($"There are {args.Length} arguments"); 的调用。这将打印您的应用程序已收到多少个参数。如果值为 0,您将获得异常,但是如果高于此值,您不应获得 System.IndexOutOfRangeException。您现在可以从任何您喜欢的地方调用它,并检查该值是否大于 0。如果不是,则说明您调用不正确。

标签: c# console-application


【解决方案1】:

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

Console app arguments, how arguments are passed to Main method

谢谢大家的cmets

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多